PDA

View Full Version : Need clock to be 1/2 hour difference


olddognewtricks
11-18-2004, 07:26 PM
Hello,
I have a javascript for three clocks.
One shows the present time based on the viewer computer clcok (good).
The second one show the time in Baghdad.
The third clock is supposed to show the time in Kabul. However, Kabul has a 1/2 hour time zone.
The script looks like this:
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function GetTime() {
var dt = new Date();
var def = dt.getTimezoneOffset()/60;
var gmt = (dt.getHours() + def);
document.clock.local.value = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()));
var ending = ":" + IfZero(dt.getMinutes());
var irq =check24(((gmt + 3) > 24) ? ((gmt + 3)- 24) : (gmt + 3));
document.clock.irq.value = (IfZero(irq) + ending);
var afg =check24(((gmt + 4) > 24) ? ((gmt + 4 ) - 24) : (gmt + 4 ));
document.clock.afg.value = (IfZero(afg) + ending);
setTimeout("GetTime()", 6000);
}
function IfZero(num) {
return ((num <= 9) ? ("0" + num) : num);
}
function check24(hour) {
return (hour >= 24) ? hour - 24 : hour;
}
// End -->
</script>

<form name="clock">
<p style="margin-top: 4; margin-bottom: -12">
<font size="1" color="#000000">LOCAL</font> <input type="text" size="3" name="local"><font size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color="#000000">BAGHDAD</font></font> <font color="#000000"> </font> <input type="text" size="3" name="irq"><font size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color="#000000">KABUL</font></font> <font color="#000000"> </font> <input type="text" size="3" name="afg">
</form>


If someone could show me how to change the script to adjust to the 1/2 time zone difference, I would be greatful.

Thanks for your help.

olddognewtricks

Willy Duitt
11-18-2004, 08:37 PM
Untested but did you try: gmt +/- .5

Roy Sinclair
11-18-2004, 09:13 PM
Something you should know about:

var dt = new Date();

After you execute that line you have a variable named dt which contains the number of milliseconds since Jan 1, 1970. If you want to adjust what it contains by 1/2 hour then just subtract 1800000 (30 minutes * 60 seconds * 1000 milliseconds) from that value.

dt -= 1800000;

Once you know that "dt" is essentially a number and that you can perform simple math on it any other adjustments are made just as easily.

Philip M
11-18-2004, 09:20 PM
I think this works.



<BODY onload="GetTime()">

<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function GetTime() {
var dt = new Date();
var def = dt.getTimezoneOffset()/60;
var gmt = (dt.getHours() + def);
var mins = (dt.getMinutes());
document.clock.local.value = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()));
var ending = ":" + IfZero(dt.getMinutes());
var iraq =check24(((gmt + 3) > 24) ? ((gmt + 3)- 24) : (gmt + 3));
document.clock.irq.value = (IfZero(iraq) + ending);
ending=(mins+30)
var addhours=3;
if (ending >60) {
ending= ending -30;
addhours=4;
}
var afg =check24(((gmt + addhours > 24) ? ((gmt + addhours ) - 24) : (gmt + addhours));
document.clock.afg.value = (IfZero(afg)+":" + ending);
setTimeout("GetTime()", 6000);
}
function IfZero(num) {
return ((num <= 9) ? ("0" + num) : num);
}
function check24(hour) {
return (hour >= 24) ? hour - 24 : hour;
}
// End -->
</script>

<form name="clock">
<p style="margin-top: 4; margin-bottom: -12">
<font size="1" color="#000000">LOCAL</font> <input type="text" size="8" name="local"><font size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color="#000000">BAGHDAD</font></font> <font color="#000000"> </font> <input type="text" size="8" name="irq"><font size="1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color="#000000">KABUL</font></font> <font color="#000000"> </font> <input type="text" size="8" name="afg">
</form>



</BODY>

olddognewtricks
11-19-2004, 10:53 PM
Hello,
The (-+.5) didn't work, that was the first thing that I tried.

Roy Sinclair, your information was good but I am a beginner and have not idea how to make the changes to the clock based on your information. I thank you for your help!

Phillip M, I did try your script but it has two errors:

<html>


<body onload="GetTime()">
<SCRIPT LANGUAGE="JavaScript">;

<!-- Begin
function GetTime() {
var dt = new Date();
var def = dt.getTimezoneOffset()/60;
var gmt = (dt.getHours() + def);
var mins = (dt.getMinutes());
document.clock.local.value = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()));
var ending = ":" + IfZero(dt.getMinutes());
var irq =check24(((gmt + 3) > 24) ? ((gmt + 3)- 24) : (gmt + 3));
document.clock.irq.value = (IfZero(irq) + ending);
var ending=(mins+30)
var addhours=3;
if (ending>60) {
ending= ending -30;
addhours=4;
}
var afg =check24(((gmt + 4 > 24) ? ((gmt + 4 ) - 24) : (gmt + 4));
document.clock.afg.value = (IfZero(afg)+":" + ending);
setTimeout("GetTime()", 6000);
}
function IfZero(num) {
return ((num <= 9) ? ("0" + num) : num);
}
function check24(hour) {
return (hour >= 24) ? hour - 24 : hour;
}
// End -->
</script>
<form name="clock">
<p style="margin-top: 4 margin-bottom: -12">
<font size="1" color="#000000"><LOCAL></font> <input type="text" size="8" name="local"><font size="1">
<font color="#000000"><BAGHDAD></font></font><font color="#000000"></font><input type="text" size="8" name="irq"><font size="1">
<font color="#000000"><KABUL></font></font><font color="#000000"></font><input type="text" size="8" name="afg">
</form>

</body>

</html>

One is line 9; object expected
The other is line 29; expected 'j'
Thank you for your help.

I hope that I am not insulting anyone on this board by offering to pay for a script, but I need a javascript for three clocks (similar to the script on the original post). I need the Kabul clock to have a +1 1/2 hour time difference from the Baghdad clock.
I am willing to pay a reasonable price for this script. Please contact me by email if you can help.
I am an old dog trying to learn new tricks...but sometimes I get tired on chasing the ball,LOL.
Thanks for all of your great help!

olddognewtricks

Philip M
11-20-2004, 07:45 AM
A closing bracket ) (not a j) seems to have got lost somewhere.
JavaScript is extremely sensitive and unforgiving!!

Try again!



<body onload="GetTime()">
<SCRIPT LANGUAGE="JavaScript">;

<!-- Begin
function GetTime() {
var dt = new Date();
var def = dt.getTimezoneOffset()/60;
var gmt = (dt.getHours() + def);
var mins = (dt.getMinutes());
document.clock.local.value = (IfZero(dt.getHours()) + ":" + IfZero(dt.getMinutes()));
var ending = ":" + IfZero(dt.getMinutes());
var irq =check24(((gmt + 3) > 24) ? ((gmt + 3)- 24) : (gmt + 3));
document.clock.irq.value = (IfZero(irq) + ending);
var ending=(mins+30)
var addhours=3;
if (ending>60) {
ending= ending -60;
addhours=4;
}
var afg =check24(((gmt + addhours > 24) ? ((gmt + addhours) - 24) : (gmt + addhours)));
document.clock.afg.value = (IfZero(afg)+":" + ending);
setTimeout("GetTime()", 6000);
}
function IfZero(num) {
return ((num <= 9) ? ("0" + num) : num);
}
function check24(hour) {
return (hour >= 24) ? hour - 24 : hour;
}
// End -->
</script>
<form name="clock">
<p style="margin-top: 4 margin-bottom: -12">
<font size="1" color="#000000"><LOCAL></font> <input type="text" size="8" name="local"><font size="1">
<font color="#000000"><BAGHDAD></font></font><font color="#000000"></font><input type="text" size="8" name="irq"><font size="1">
<font color="#000000"><KABUL></font></font><font color="#000000"></font><input type="text" size="8" name="afg">
</form>


I understood you wanted Kabul to be 30 mins ahead of Baghdad. If it is 90 mins ahead (in advance - later in the day) you need to change

var addhours=3;
to var addhours=4;
and
addhours=4;
to
addhours=5;

olddognewtricks
11-20-2004, 04:37 PM
Phillip M,
Thank you so much, the script is perfect!
I want you all to know that I appreciate all of the help that you guys give. Without your help my web site would look like a three year old did it.

Have a safe and happy holiday!

olddognewtricks

Philip M
11-20-2004, 09:16 PM
Don't mention it, we are all glad to assist, but as always
it is nicer when people express their appreciation afterwards
(as you have done) rather than "in advance".