Daylight Saving Time

Daylight saving time is something that only applies in some places. The purpose is to shift hours of daylight from early morning when most people are asleep into the evening when they are more likely to be awake to benefit from the daylight.

It doesn't apply close to the equator as there the day doesn't vary greatly in length and so there isn't much if any daylight prior to people waking up to move. It doesn't apply close to the poles because there the daylight soon extends into the evening without needing to borrow some from the early morning. Daylight saving time only applies in some of those places in between where the day varies enough in length to provide early morning daylight that can effectively be moved to the evening so as to provide more daylight when most people are awake.

JavaScript doesn't directly provide us with information as to whether or not our visitor is using daylight saving time. All that JavaScript provides with respect to information about our visitor's local time is the timezone that they are in. What is not quite so obvious is that JavaScript does take daylight saving time as defined on their computer into account in determining what value to return in that offset.

Modern operating systems can switch the computer between standard time and daylight saving time automatically based on information as to when daylight saving time starts and finishes. When JavaScript references a specific date the timezone offset field will be set appropriately for that date based on the information in the computer as to whether that date was or wasn't on daylight saving time.

That the offset changes depending on whether or not daylight saving applies is sufficient for us to determine whether they are currently on daylight saving time. Since where daylight saving time does apply, it applies for a period during summer and doesn't apply for a period in winter. The exact start and end dates vary from place to place and which part of the year is summer and which winter depends on which hemisphere you are in. What we can reasonably say based on this though is that if we pick two dates where one is in the middle of summer and the other in the middle of winter that they will be in different timezones if the place uses daylight saving time.

If we take the timezone offsets that apply on 1st January and 1st July of any year then if they are both the same then we can reasonably say that the place doesn't use daylight saving time. If they are different then the earlier timezone will be the standard timezone and the later one (where the clocks have been moved forward) will be the daylight saving timezone. So where the current timezone offset is not the same as the earlier of those two offsets they are on daylight saving time. To achieve this in JavaScript we can add two methods to our date objects, the first to retrieve the standard time timezone offset (being the earlier offset from the two dates six months apart) and a second that returns true or false depending on whether they are on daylight saving time or not.

Date.prototype.stdTimezoneOffset = function() {
var jan, jul;
jan = new Date(this.getFullYear(), 0, 1);
jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
Date.prototype.dst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow