I had to work with timezone on the project I'm working on and I hate it. So I created jsKata.timezone to deal with them (view documentation).

How does it work?

In short, this library doesn't do a lot but saves you a lot of troubles. It returns the standard time offset of the user's browser. From there, you can narrow the list of possible time zones for a user. To have the standard timezone offset simple call :

var standardOffset = jsKata.timezone.st();
  /* or you can also call a shorter version */ 
  standardOffset = jsk.tz.st();

You can see a demo.

Other functions

// Return the daylight saving time offset
  var daylightSavingOffset = jsKata.timezone.dst();
  
  // If the time zone has daylight saving
  jsk.tz.hasDst();
  
  // Return the standard time zone as a string (ex : -0500)
  jsk.tz.stToString();
  
  // Return the daylight saving time zone as a string (ex : -0400)
  jsk.tz.dstToString();

Under the hood

You can't have a named time zone in javascript (example : eastern time or central time), you can only have a time zone offset which is represented by universal time (UTC) minus the distance in minutes to it by calling dateVariable.getTimezoneOffset(). It means that if the time zone offset is -1 hours of UTC, javascript will give you 60. Why is it inverted in javascript? I have no idea.

In winter, it's always standard time. In summer, it's daylight saving time which is standard time minus 60 minutes… but not for every country. Plus, summer and winter are inverted in the southern hemisphere. That's a lot of exceptions and that's why I created the jsKata.timezone library.

comments powered by Disqus