CSGNetwork.com Free Information


Time Zone Offset Calculations

The information on this page deals with how to determine time zone information while manipulating time and date information within JavaScript. There are several built in functions within JavaScript to get and read time information but they fall short in many areas. Some manual calculations can be done to fill in the gaps. A common problem is that of DST observance. The most logical resolution approach is to simulate two different times of year on calls for time in relationship to GMT (UTC). In the winter, DST (Daylight Saving Time) is not observed; however, in some areas during the summer months, DST is in effect. So, by comparing a winter time to a summer time to determine if the number of hours is DIFFERENT as to the offset from GMT, we can somewhat adequately determine if DST or a similar observance scheme is used within that time zone. (Validity is not assured in that even though DST or a similar change scheme is observed within the zone, often, not all locations within that zone comply.) You may use this code to build a test routine to serve your own purpose as it does work with both the old and new methods of determining the DST times. This page requires the use of Javascript enabled and capable browsers.

NOTE: This pre-dates 2007 DST times. See the JavaScript detector snippet (you can use the code) for new DST rules for the US that began in 2007.


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(" ")));
   var temp = date2.toGMTString();
   var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")));
   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.");
   }
}


JavaScript Code Explanation

The variable date1 is initially set to the 1st of January (month 0 in JavaScript) this year. The variable date2 is initially set to the 1st of July (month 6 in JavaScript) 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 computer's 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. By your computer information, this difference is .

All JavaScript dates are in milliseconds, so 1000 milliseconds times 60 seconds times 60 minutes is the number of milliseconds in one hour. This is the methodology used to compare the hours. The check of time in January sets the benchmark for winter for both old and new methods of determination.

The summer time check serves the same purpose in both old and new methods, establishing the benchmark for summer comparison. If the calculated number of hours difference changes from January to July, then daylight saving time is observed in this time zone. (However, keep in mind that not all locations within a specific time zone honor DST.) Based on the information provided by your computer and login, daylight saving timeobserved in this time zone.

This code is valid in time zones that are partial (not full hour differences) also, such as a quarter hour or a half hour difference from GMT, in some parts of the world. The hoursDiffStdTime variable value will obviously not be a whole number; the fractional part will be .25, .5, or .75 depending on the time zone value. Pay particular attention in your coding NOT to make that variable an integer. The button below will test the actual Javascript code within this page. It is identical code implementation to what is shown above.


Version 1.0.3


Leave us a question or comment on Facebook
Search or Browse Our Site
Free Information Calculators and Converters

International Copyright Violation
Registered® Trademark™ and Copyright© 1973 - CSG, Computer Support Group, Inc. and CSGNetwork.Com All Rights Reserved