Click here to Skip to main content
14,237,394 members

JavaScript Code to Determine When DayLight Savings Time (DST) Occurs

Rate this:
3.88 (11 votes)
Please Sign up or sign in to vote.
3.88 (11 votes)
19 Feb 2010CPOL
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 information.

<!DOCTYPE html>
    <title>DST Calculator</title>
    <script type="text/javascript">

    function DisplayDstSwitchDates()
        var year = new Date().getYear();
        if (year < 1000)
            year += 1900;

        var firstSwitch = 0;
        var secondSwitch = 0;
        var lastOffset = 99;

        // Loop through every month of the current year
        for (i = 0; i < 12; i++)
            // Fetch the timezone value for the month
            var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0));
            var tz = -1 * newDate.getTimezoneOffset() / 60;

            // Capture when a timzezone change occurs
            if (tz > lastOffset)
                firstSwitch = i-1;
            else if (tz < lastOffset)
                secondSwitch = i-1;

            lastOffset = tz;

        // Go figure out date/time occurrences a minute before
        // a DST adjustment occurs
        var secondDstDate = FindDstSwitchDate(year, secondSwitch);
        var firstDstDate = FindDstSwitchDate(year, firstSwitch);

        if (firstDstDate == null && secondDstDate == null)
            return 'Daylight Savings is not observed in your timezone.';
            return 'Last minute before DST change occurs in ' +
               year + ': ' + firstDstDate + ' and ' + secondDstDate;

    function FindDstSwitchDate(year, month)
        // Set the starting date
        var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0));
        var changeDay = 0;
        var changeMinute = -1;
        var baseOffset = -1 * baseDate.getTimezoneOffset() / 60;
        var dstDate;

        // Loop to find the exact day a timezone adjust occurs
        for (day = 0; day < 50; day++)
            var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));
            var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

            // Check if the timezone changed from one day to the next
            if (tmpOffset != baseOffset)
                var minutes = 0;
                changeDay = day;

                // Back-up one day and grap the offset
                tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0));
                tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

                // Count the minutes until a timezone change occurs
                while (changeMinute == -1)
                    tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0));
                    tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

                    // Determine the exact minute a timezone change
                    // occurs
                    if (tmpOffset != baseOffset)
                        // Back-up a minute to get the date/time just
                        // before a timezone change occurs
                        tmpOffset = new Date(Date.UTC(year, month,
                                             day-1, 0, minutes-1, 0, 0));
                        changeMinute = minutes;

                // Add a month (for display) since JavaScript counts
                // months from 0 to 11
                dstDate = tmpOffset.getMonth() + 1;

                // Pad the month as needed
                if (dstDate < 10) dstDate = "0" + dstDate;

                // Add the day and year
                dstDate += '/' + tmpOffset.getDate() + '/' + year + ' ';
                // Capture the time stamp
                tmpDate = new Date(Date.UTC(year, month,
                                   day-1, 0, minutes-1, 0, 0));
                dstDate += tmpDate.toTimeString().split(' ')[0];
                return dstDate;

        <script type="text/javascript">
            document.write("Current date/time: " + new Date() + "<br />");

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.

Comments and Discussions

SuggestionExecution seems too expensive Pin
pcdonp5-Apr-14 22:38
memberpcdonp5-Apr-14 22:38 
GeneralRe: Execution seems too expensive Pin
Matt Esterak18-Jun-14 20:24
memberMatt Esterak18-Jun-14 20:24 
GeneralRe: Execution seems too expensive Pin
Mcshaz20-Apr-15 23:06
memberMcshaz20-Apr-15 23:06 
GeneralSnashot Has Incorrect Dates Pin
PRISMAY21-Dec-10 3:10
memberPRISMAY21-Dec-10 3:10 
GeneralRe: Snashot Has Incorrect Dates Pin
Matt Esterak21-May-11 22:29
memberMatt Esterak21-May-11 22:29 
QuestionCorrections to question Pin
Robert Adamo15-Oct-10 8:30
memberRobert Adamo15-Oct-10 8:30 
QuestionHelp!!!! Pin
Robert Adamo15-Oct-10 8:27
memberRobert Adamo15-Oct-10 8:27 
Hey Matt
Hopefully the Help got your attention. I need your help. Your script is fantastic and I would like to incorporate it into my website. But I cannot for the life of me get it to work in an 3.5 environment VS 2008. Nothing seem to work in allow me to use a asp label of hidden field instead of document.write so that I can control where I place the information on the page and also to be able to send it back to the server for future use. I tried using document.getElementById("HiddenFieldClientTime") and document.getElementById("Label1") and
var hdnControl=document.getElementById("HiddenFieldClientTime.ClientID") and var hdnControl=document.getElementById("Label1.ClientID").
Can you help. My objective is to post your code on the home page and when the user logs in and a post back occurs I can then store the timezone information in a session or DB.

AnswerRe: Help!!!! Pin
Matt Esterak21-May-11 22:18
memberMatt Esterak21-May-11 22:18 
GeneralIssues Pin
SmirkinGherkin16-Feb-10 6:09
memberSmirkinGherkin16-Feb-10 6:09 
GeneralRe: Issues Pin
Matt Esterak18-Feb-10 17:15
memberMatt Esterak18-Feb-10 17:15 
GeneralRe: getYear() Pin
Francis Uy18-Mar-10 0:49
memberFrancis Uy18-Mar-10 0:49 
GeneralRe: getYear() Pin
Matt Esterak19-Mar-10 9:26
memberMatt Esterak19-Mar-10 9:26 
GeneralRe: Issues Pin
Matt Esterak18-Feb-10 18:28
memberMatt Esterak18-Feb-10 18:28 
GeneralRe: Issues Pin
SmirkinGherkin18-Feb-10 21:55
memberSmirkinGherkin18-Feb-10 21:55 
GeneralMy vote of 1 Pin
Md. Marufuzzaman14-Feb-10 5:28
mentorMd. Marufuzzaman14-Feb-10 5: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.

Posted 14 Feb 2010

Tagged as


11 bookmarked