Time Zones
The subject of time zones with respect to web pages doesn't come up very often. But when it does there's little documentation on the internet available concerning calculating time zones in JavaScript. Basically, you have the user's current time and that can be converted to Greenwich Mean Time (GMT). But it could be summer with daylight saving time (note: it is not "savings" but is, in fact, "saving") in effect. This would give a time zone of one hour off. To check for everything, make two comparisons against GMT: one in January and one in July. Here is some JavaScript code that will print out the time zone offset and whether daylight saving time is used in this time zone or not. function checkTimeZone() {
var rightNow = new Date();
var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
var temp = date1.toGMTString();
var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var temp = date2.toGMTString();
var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
if (hoursDiffDaylightTime == hoursDiffStdTime) {
alert("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is NOT observed here.");
} else {
alert("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is observed here.");
}
}
The variable date1 is initially set to the 1st of January (month 0) this year. The variable date2 is initially set to the 1st of July (month 6) this year. Then date1 is converted to a GMT string. This format will be something like Fri, 25 Apr 2003 00:00:00 UTC. In fact, your clock as of when this page was loaded gives a GMT string of . The variable date3 pulls out everything except the time zone from that string. (The time zone says "UTC", and we want this date to be in the local time zone). So that GMT time (in the local time zone) minus the actual time (in the local time zone) is the number of hours away from GMT we are at this point. For you, this difference is .
Note the check for the hours difference. JavaScript dates are in milliseconds, so 1000 milliseconds times 60 seconds times 60 minutes is the number of milliseconds in one hour.
The other check does the same thing, except in July instead of January. If the number of hours difference changes from January to July, then daylight saving time is observed in this time zone. For you, daylight saving timeobserved here.
Note that this code has not been checked in time zones that are either a quarter hour or a half hour away from GMT, like in some parts of the world. But the code should work in those time zones. The hoursDiffStdTime variable should just not be a whole number - the fractional part will be .25, .5, or .75 depending on the time zone.