Click to See Complete Forum and Search --> : Timezones clock with date, Help plz, can't find in history


Kredo
05-08-2003, 04:05 PM
Hello, I know that this must be a common question on the board and prolly got annoying but before posting this I searched for what I need and couldn't find.

I just need the script with 3 simple different timezones showing in military time format. I looked at Dave's script and wasn't able to adjust it for what I need. Why I need for 3 timezones is because I'm going to implement this script on the message board where people from 3 different time zones usually post. Along with the time I need a Date. The timezones are -5GMT, +3GMT and +10GMT. I really appreciate If someone could point me to the right place or help me out with this.

Thanks

Kredo

Charles
05-08-2003, 04:21 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
<!--
Date.prototype.toString = function () {return [this.getUTCHours(), this.getUTCMinutes() < 10 ? '0' + this.getUTCMinutes() : this.getUTCMinutes(), this.getUTCSeconds() < 10 ? '0' + this.getUTCSeconds() : this.getUTCSeconds()].join(':')};

Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;

document.write('<form name="time"><div>');
document.write('<p>GMT</br><input type="text" name="GMT"></p>');
document.write('<p>-5GMT</br><input type="text" name="one"></p>');
document.write('<p>+3GMT</br><input type="text" name="two"></p>');
document.write('<p>+10GMT</br><input type="text" name="three"></p>');
document.write('</div></form>');

setInterval ('document.forms.time.GMT.value = new Date(); document.forms.time.one.value = new Date(new Date().getTime() - 5 * Date.ONE_HOUR); document.forms.time.two.value = new Date(new Date().getTime() + 3 * Date.ONE_HOUR); document.forms.time.three.value = new Date(new Date().getTime() + 10 * Date.ONE_HOUR)', 0.25 * Date.ONE_SECOND);

// -->
</script>

Kredo
05-08-2003, 07:51 PM
Charles, I tried your script and it's really perfect. But is there any way to fix this 1 or make it simpler? I know there is lots of unneeded stuff in it since I'm not that good in Javascript but that's what I tried and it still doesn't work right.

<html>

<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: John Walker (john@321webliftoff.net) -->
<!-- Web Site: http://321webliftoff.net -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function isodatetime() {
var today = new Date();
var year = today.getYear();
if (year < 2000) // Y2K Fix, Isaac Powell
year = year + 1900; // http://onyx.idbsu.edu/~ipowell
var month = today.getMonth() + 1;
var day = today.getDate();
var hour = today.getHours();
var hourUTC = today.getUTCHours();
var diff = hour - hourUTC;

var dayV=day
var dayM=day
var dayNY=day

var monthV=month
var monthM=month
var monthNY=month

var hourdifference = Math.abs(diff);


var vlad = hourUTC + 11;
if (vlad > 23||hour+hourdifference>23)
vlad = vlad - 24, dayV=day + 1
if (vlad<0)
vlad = vlad+24;

var moscow = hourUTC + 4;
if (moscow>23||hour+hourdifference>23)
moscow = moscow - 24, dayM=day+1;
if (moscow<0)
moscow = moscow+24;

var newyork = hourUTC - 4;
if (newyork>23||hour+hourdifference>23)
newyork = newyork - 24, dayNY=day+1;
if (newyork<0)
newyork = newyork+24;


<!-- dayV -->

if (monthV==12&&dayV==32) {
dayV=dayV-31, monthV=month-11, year=year+1;
}
if (dayV==32)
dayV=dayV-31, monthV=month+1

if (monthV==2&&dayV==29) {
dayV=dayV-28, monthV=month+1;
}
if (monthV==4&&dayV==31) {
dayV=dayV-30, monthV=month+1;
}
if (monthV==6&&dayV==31) {
dayV=dayV-30, monthV=month+1;
}
if (monthV==4&&dayV==31) {
dayV=dayV-30, monthV=month+1;
}
if (monthV==9&&dayV==31) {
dayV=dayV-30, monthV=month+1;
}
if (monthV==11&&dayV==31) {
dayV=dayV-30, monthV=month+1;
}
<!-- dayM -->

if (monthM==12&&dayM==32) {
dayM=dayM-31, month=month-11, year=year+1;
}
if (dayM==32)
dayM=dayM-31, monthM=month+1

if (monthM==2&&dayM==29) {
dayM=dayM-28, monthM=month+1;
}
if (monthM==4&&dayM==31) {
dayM=dayM-30, monthM=month+1;
}
if (monthM==6&&dayM==31) {
dayM=dayM-30, monthM=month+1;
}
if (monthM==4&&dayM==31) {
dayM=dayM-30, monthM=month+1;
}
if (monthM==9&&dayM==31) {
day=day-30, monthM=month+1;
}
if (monthM==11&&dayM==31) {
dayM=dayM-30, monthM=month+1;
}

<!-- dayNY -->

if (monthNY==12&&dayNY==32) {
dayNY=dayNY-31, monthNY=month-11, year=year+1;
}
if (dayNY==32)
dayV=dayNY-31, monthNY=month+1

if (monthNY==2&&dayNY==29) {
dayNY=dayNY-28, monthNY=month+1;
}
if (monthNY==4&&dayNY==31) {
dayNY=dayNY-30, monthNY=month+1;
}
if (monthNY==6&&dayNY==31) {
dayNY=dayNY-30, monthNY=month+1;
}
if (monthNY==4&&dayNY==31) {
dayNY=dayNY-30, monthNY=month+1;
}
if (monthNY==9&&dayNY==31) {
dayNY=dayNY-30, monthNY=month+1;
}
if (monthNY==11&&dayNY==31) {
dayNY=day-30, monthNY=month+1;
}

var hourdifference = Math.abs(diff);
var minute = today.getMinutes();
var minuteUTC = today.getUTCMinutes();
var minutedifference;
var second = today.getSeconds();



timeV = "Vladivostok " + vlad + ":" +minute+ ":" +second+ " "+day+"-"+month+"-"+year;
timeM = "Moscow " + moscow +":"+minute+ ":" +second+ " "+dayM+"-"+month+"-"+year;
timeNY = "NewYork " + newyork +":"+minute+ ":" +second+ " "+dayNY+"-"+month+"-"+year;

document.isoclock.display.value = timeV;
document.isoclockM.display.value = timeM;
document.isoclockNY.display.value = timeNY;

window.setTimeout("isodatetime();", 500);
}
// End -->


</script>





<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 7</title>
</head>

<body>
<BODY OnLoad="isodatetime()">



<center>
<form name=isoclock>
<input type=text name=display size=30 style="border:0px none;">
</form>
<form name=isoclockM>
<input type=text name=display size=30 style="border:0px none;">
</form>
<form name=isoclockNY>
<input type=text name=display size=30 style="border:0px none;">
</form>





<p><center>
<font face="arial, helvetica" size="-2">Free JavaScripts provided<br>
by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
</center><p>



</body>

</html>

Charles
05-08-2003, 09:16 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<style type="text/css">
<!--
div.time {font-family:georgia; font-size:90%; text-align:center}
div.time input {border:0px; font-family:georgia; font-size:90%; margin-left:1em}
-->
</style>
<script type="text/javascript">
<!--
Date.prototype.toString = function () {return [this.getUTCHours(), this.getUTCMinutes() < 10 ? '0' + this.getUTCMinutes() : this.getUTCMinutes(), this.getUTCSeconds() < 10 ? '0' + this.getUTCSeconds() : this.getUTCSeconds()].join(':') + ' ' + [this.getUTCMonth() + 1, this.getUTCDate(), this.getUTCFullYear()].join('-')};

Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;

document.write('<form name="time"><div class="time">');
document.write('<p>GMT<input type="text" name="GMT"></p>');
document.write('<p>-5GMT<input type="text" name="one"></p>');
document.write('<p>+3GMT<input type="text" name="two"></p>');
document.write('<p>+10GMT<input type="text" name="three"></p>');
document.write('</div></form>');

setInterval ('document.forms.time.GMT.value = new Date(); document.forms.time.one.value = new Date(new Date().getTime() - 5 * Date.ONE_HOUR); document.forms.time.two.value = new Date(new Date().getTime() + 3 * Date.ONE_HOUR); document.forms.time.three.value = new Date(new Date().getTime() + 10 * Date.ONE_HOUR)', 0.20 * Date.ONE_SECOND);

// -->
</script>

Note: typo corrected.

Kredo
05-08-2003, 09:22 PM
AWESOME! Thank you very much Charles! I really appriciate it :)

Kredo
05-08-2003, 10:06 PM
1 more question, probably a bit harder. What if they have checked "Automatically adjust clock for daylight saving changes" in windows clock? I tried it and the time is off by 1 hour this way. Any way to fix that or they'll just have to deal with that?

Charles
05-09-2003, 05:10 AM
It shouldn't make any difference. JavaScript represents internally date/times as the number of milliseconds from 1 January 1970 UTC, but ignoring leap seconds. (With JavaScript you get UTC time and not GMT time but the difference is less than the inaccuracy of your computer clock.) If your local time zone is set incorrectly and you have set your local time to compensate then those scripts will be off. There was an embarrassing error in my second version, but I've corrected it.

Kredo
05-09-2003, 12:17 PM
what was the mistake? Is it something important in the script?I already put the script on the site so what should I correct?

Charles
05-09-2003, 12:35 PM
Date.prototype.toString = function () {return [this.getUTCHours(), this.getUTCMinutes() < 10 ? '0' + this.getUTCMinutes() : this.getUTCMinutes(), this.getUTCSeconds() < 10 ? '0' + this.getUTCSeconds() : this.getUTCSeconds()].join(':') + ' ' + [this.getUTCMonth() + 1, this.getUTCDate(), this.getUTCFullYear()].join('-')};

Take a look at the month. At first I had that as - 1 and the month was off by two. If your page is working then I corrrected my error before it caused any harm to you and I am glad.

Kredo
05-09-2003, 01:36 PM
Is there a script to detect if the user clock has this "adjust for daylight saving" checked or not? The thing is that those users that have it checked and have the right time on the computer, the script shows 1 by hour off.

I'm in the -5 zone right now and it's 2:40PM but if I have checked the "daylight saving" then the script shows that it's 1:40PM.

Any way to fix that or should I just configure it considering all users have the daylight saving checked?

Charles
05-09-2003, 02:24 PM
That's because they then have their time set incorrectly. I wouldn't worry about it but if you find that you must then use SSI or some other server side method to include the time at the server and then use that to figure out if the clients time is off.

Charles
05-10-2003, 10:13 AM
Preliminary testing indicates that this approach solves for your problem.

<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;

Date.prototype.toUTCOffset = function (offset) {return new Date(this.getTime() + this.getTimezoneOffset() * 60000 + offset * Date.ONE_HOUR)}

Date.prototype.toTimeString = function () {return [this.getHours() < 13 ? this.getHours() : this.getHours() - 12, this.getMinutes() < 10 ? '0' + this.getMinutes() : this.getMinutes(), this.getSeconds() < 10 ? '0' + this.getSeconds() : this.getSeconds()].join (':') + (this.getHours() < 13 ? ' AM' : ' PM')}

document.write('<p id="time">-4 GMT ', new Date().toUTCOffset(-4).toTimeString(), '</p>');
if (document.getElementById) setInterval("document.getElementById('time').innerHTML = '-4 GMT ' + new Date().toUTCOffset(-4).toTimeString()", 0.2 * Date.ONE_SECOND);

// -->
</script>

Kredo
05-10-2003, 01:20 PM
what's that? Script with daylight saving changes correction?