1. Technology
Send to a Friend via Email

Daylight Saving Time

Adding Methods to the Date Object

One flag missing from the information that the JavaScript date object holds regarding the current date and time is whether or not the time is for the standard timezone for your location or whether it is on daylight saving time.

The only information that JavaScript does make available is the current timezone offset and what the GMT time is based on that offset. The computer itself knows whether the timezone offset it supplies to the browser accounts for DST or not but that information is not directly available to JavaScript.

In those parts of the world far enough from the equator to make the use of daylight savings time worthwhile that timezone shift is applied for a number of months during summer and is not applied during winter. We can make use of this fact to add a couple of extra methods to the JavaScript date object that will allow us to determine whether a given date and time is or isn't daylight saving time.

What we are going to do is to select two dates - the first of January and the first of July. In those places where daylight saving time is used we are going to assume that daylight saving time is not in force on one of these two dates. Provided that this assumption is correct for everywhere that uses daylight saving time then the following methods will function correctly. If there is anywhere in the world that applies daylight saving time on both of those dates then our code will fail.

The first thing we are going to do is to create a method that will return the standard time timezone offset regardless of whether the current time is on standard or daylight saving time. Note that JavaScript returns the offset in minutes and reverses the sign (so for example time zone +10 will return an offset of -600).

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

Provided that one of those two dates is not on daylight savings time that method will return the timezone from that date (since a day that is on daylight saving time will have an offset that is one greater since the clocks are moved forward when daylight saving applies eg Sydney standard timezone is 10 and DST is 11, New York standard timezone is -5 and DST is -4).

Now that we have a way to obtain the standard time timezone offset we have a relatively simple way of determining if the current time is on daylight saving time or not. We simply compare the current timezone offset with the standard one. If they are equal then the current time is standard time. If they are not then the current time is daylight saving time.

Date.prototype.dst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

This second method will return true when the current time is daylight saving time and false when it is standard time. (Again note that the comparison is the opposite of what you would expect due to JavaScript reversing the sign on the offset).

What about those places that don't use daylight saving time? Well our methods already handle that too. Where that is the case the two timezone offsets the first method looks at will be the same and therefore the minimum of the two will still give us the standard timezone offset. Since the offset throughout the year will always be that same value our second method will therefore always return false.

So let's end with a working example of the script in action. The following paragraph will update automatically so as to be correct for the timezone you have your computer set to. It does it simply by creating a date object set to today's date (var today = new Date();) and then tests if we need to remove the word "not" from the sentence (if (today.dst()) ...).

Where you are is currently not on daylight saving.

See More About
Related Video
Maximize Evening Quality Time

©2014 About.com. All rights reserved.