Javascript Check For Daylight Saving

Trying to work out the correct DateTime offset of a visitor can be tricky, the easiest way of doing this would be with JavaScript. You can find out the visitors DateTime information by using the JavaScript Date object.

var currentOffset = today.getTimezoneOffset(); This will return the amount of minutes the current timezone is offset from GMT. Therefore to get the offset in hours you'll need to divide by 60.

var today = new Date();

var currentOffsetInHours = (today.getTimezoneOffset() / 60); But the offset could be different depending on the time of the year you are trying to access this information. For example the browser will know if the given timezone is in daylight saving or not so if the visitor accessed the page in January the offset could be different than when accessing the page in July. First we need to create a function to return the largest of the offsets based on January and July.

The getTimezoneOffset() method will return the offset in minutes and will reverse the offset sign therefore if the offset is GMT+1 getTimezoneOffset() will return -60. In my example based on London timezone, the January offset will equal 0 whereas the July offset will equal -60. jan.getTimezoneOffset() = 0 jul.getTimezoneOffset() = -60 The return of the new stdTimezoneOffset function will return the highest number of offset, this being the January offset of 0. Then we compare the return of the stdTimezoneOffset function with the current offset. If the current offset is lower then we know that it's currently using daylight savings time.