Click here to Skip to main content
12,695,049 members (33,870 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked

JavaScript code to determine when DayLight Savings Time (DST) occurs

, 19 Feb 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
JavaScript code to determine when DayLight Savings Time (DST) occurs.



Sometimes you need to be able to determine when Daylight Savings Time (DST) will occur in a given year. DST varies around the world for timezones where DST is observed. The code herein can automatically determine the date/time a minute before a DST change occurs in the user's timezone. The JavaScript Date object is used as part of calculating the date/time just before DST adjustments occur. I needed a solution with this behavior to properly adjust UTC date/time for previous and future dates intended for display to users. Feel free to adapt the code for your own uses.

Using the Code

The DST determination is split between two functions. The DisplayDstSwitchDates() function locates the initial date/time for each month before a timezone change occurs. This method internally calls the FindDstSwitchDate() method, which in turn determines the exact minute before a timezone change occurs.

To elaborate more on the code, let's first take a look at the DisplayDstSwithDates() function. This function is very simple. Its only purpose is to get the date/time from 12 consecutive months and compare the timezone offset month to month. When the timezone changes, the month prior to the current date/time checked is captured. Typically, there are two times the timezone will change if the code is running on a browser running in a geographical location where DST is observed. The for loop completes once the month counter (variable i) reaches 12. With the current year and the two month numbers captured, the FindDstSwitchDate() function is called.

The FindDstSwitchDate() function may appear busy, but it is actually very simple. The first part of the function starts checking the timezone offset day by day, beginning with the month and year supplied as function parameters. Once the timezone offset changes, the day prior to the timezone offset change is captured. The rest of the function counts minute by minute, beginning with the day captured, until the timezone offset changes. The minute prior to the actual date/time that the timezone offset changes is captured. Using the captured day and number of minutes, the function assembles a string to represent the date/time that is one minute prior to when a timezone offset change occurs. This function is called twice for locations where DST is observed. One date represents a minute before when you turn the clock back an hour, and the other date represents a minute before when you move the clock ahead by an hour.

If you would like to learn more about DST and see the applicable DST rules for countries around the world, please have a look here: time and DST info.

<!DOCTYPE html>
    <title>DST Calculator</title>

Points of Interest

Please note that capturing the date/time one minute before a timezone change occurs is purposeful in that if you try to capture the date/time one minute later, you'll find that JavaScript applies the DST time adjustment (moves the hour ahead or backward). I am open to constructive criticism, suggestions, and requests, so please provide feedback.


  • v1.0: 02-14-2010: Initial post.
  • v1.1: 02-14-2010: Added additional explanations for the code.
  • v1.2: 02-18-2010: Added a conditional to add 1900 to the year to accommodate Firefox, Chrome, and perhaps other browsers.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Matt Esterak
Engineer Intel Corporation
United States United States
I am an Automation Engineer specializing in application and web development/support.

You may also be interested in...

Comments and Discussions

SuggestionExecution seems too expensive Pin
pcdonp5-Apr-14 23:38
memberpcdonp5-Apr-14 23:38 
GeneralRe: Execution seems too expensive Pin
Matt Esterak18-Jun-14 21:24
memberMatt Esterak18-Jun-14 21:24 
GeneralRe: Execution seems too expensive Pin
Mcshaz21-Apr-15 0:06
memberMcshaz21-Apr-15 0:06 
GeneralSnashot Has Incorrect Dates Pin
PRISMAY21-Dec-10 4:10
memberPRISMAY21-Dec-10 4:10 
GeneralRe: Snashot Has Incorrect Dates Pin
Matt Esterak21-May-11 23:29
memberMatt Esterak21-May-11 23:29 
QuestionCorrections to question Pin
Robert Adamo15-Oct-10 9:30
memberRobert Adamo15-Oct-10 9:30 
QuestionHelp!!!! Pin
Robert Adamo15-Oct-10 9:27
memberRobert Adamo15-Oct-10 9:27 
AnswerRe: Help!!!! Pin
Matt Esterak21-May-11 23:18
memberMatt Esterak21-May-11 23:18 
GeneralIssues Pin
SmirkinGherkin16-Feb-10 7:09
memberSmirkinGherkin16-Feb-10 7:09 
GeneralRe: Issues Pin
Matt Esterak18-Feb-10 18:15
memberMatt Esterak18-Feb-10 18:15 
GeneralRe: getYear() Pin
Francis Uy18-Mar-10 1:49
memberFrancis Uy18-Mar-10 1:49 
GeneralRe: getYear() Pin
Matt Esterak19-Mar-10 10:26
memberMatt Esterak19-Mar-10 10:26 
GeneralRe: Issues Pin
Matt Esterak18-Feb-10 19:28
memberMatt Esterak18-Feb-10 19:28 
GeneralRe: Issues Pin
SmirkinGherkin18-Feb-10 22:55
memberSmirkinGherkin18-Feb-10 22:55 
GeneralMy vote of 1 Pin
Md. Marufuzzaman14-Feb-10 6:28
mvpMd. Marufuzzaman14-Feb-10 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 19 Feb 2010
Article Copyright 2010 by Matt Esterak
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid