Javascript: Figuring out daylight savings time.

Updated 11/2/2009: Fixed bug with code pointed out by comment.

A while back I had been dealing with the different timezones across the US.  I needed to know whether or not the central timezone was in daylight savings time or not using my current timestamp from my computer. While this ultimately ended up not mattering because I switched everything to a GMT timestamp and use that for comparisons I did write a small bit of javascript code to determine this information for me. So below you will see the rough code I hacked together which should accurately determine if we are currently in daylight savings time based on the current dates set by the US government.

function isDaylight() {

    // 2nd sunday of march
    oDate = new Date();
    var dstStartDate = new Date();
    dstStartDate.setMonth(2);
    dstStartDate.setDate(1);
    dstStartDate.setYear(2009);
    
    dateIdx = dstStartDate.getDay() ? 8 - dstStartDate.getDay() : 1;
    dstStartDate.setDate(dateIdx + 7);

    // first sunday of november
    var dstEndDate = new Date();
    dstEndDate.setMonth(10);
    dstEndDate.setDate(1);
    dstEndDate.setYear(2009);
    
    dateIdx = dstEndDate.getDay() ? 8 - dstEndDate.getDay() : 1;
    dstEndDate.setDate(dateIdx);
  
    if ((oDate.getMonth() > dstStartDate.getMonth()) && (oDate.getMonth() < dstEndDate.getMonth())) {
        bInDST = true;
    } else if (oDate.getMonth() == dstStartDate.getMonth()) {
        if (oDate.getDate() >= dstStartDate.getDate()) {
            bInDST = true;
        } else {
            bInDST = false;
        }
    } else if (oDate.getMonth() == dstEndDate.getMonth()) {
        if (oDate.getDate() < dstEndDate.getDate()) {
            bInDST = true;
        } else {
            bInDST = false;
        }
    } else {
        bInDST = false;
    }
    return bInDST;
}
Show Me
Click the button to see the code working.

Tags: , , , , ,

Thursday, April 30th, 2009 interesting, javascript, programming, tech

9 Comments to Javascript: Figuring out daylight savings time.

  1. I found the code very useful, but I think there’s an error: You have bInDST returning “false” when it should be “true”, and vice versa — assuming you want bInDST to signify that an area *is* in DST.

  2. Steve Ray on July 9th, 2009
  3. Steve, you are right I did mess that up thanks for pointing it out. I am glad the code was of use to you. I have had an injury that has put a dampen on my home coding but soon I am going to be posting up some ExtJS library usage examples when I get better. Happy coding.

  4. ben on July 9th, 2009
  5. Code updated and fixed.

  6. ben on July 28th, 2009
  7. I too found the code useful, but I think you have an error when determining the correct Sunday. If 3/1/xxxx or 11/1/xxxx is a Sunday your script is off by a week, at least when I run it. I think you need to check that the first day of the month is not a Sunday, if so it throws off your code.

  8. matt on November 2nd, 2009
  9. Thanks for your comment Matt I will take a look when I get a chance sorry for the bug.

    Will change when I get a free moment.

  10. ben on November 2nd, 2009
  11. Matt corrected code has been posted.

  12. ben on November 2nd, 2009
  13. Your also doesn’t seem to take into account the hour of the day. Daylight savings switch happens at 2AM… So there is some time in the given day that is different than the rest.

    I found a different way to determine whether a given Date Object in javascript is DST or Standard. If you’re interested, the “website” field has my blog post about it. The blog post is long and explains the circumstances… you can just scroll to the code near the bottom.

  14. Kevin on November 3rd, 2009
  15. I do not handle the 2AM switch over. This would be a minor bug. I checked your solution that would work I have never looked at the toTimeString() in javascript. Here is his blogpost for those who are interested in his solution.

    http://highdex.blogspot.com/2009/10/tech-daylight-savings-time-in.html

  16. ben on November 4th, 2009
  17. Thanks for this code. I am using it with some modification to determine DST for any year, not just 2009. This has saved me countless hours! Much, much appreciated!

    ~Jonathan
    Senior Developer

  18. Jonathan on November 16th, 2009

Leave a comment

Search

 

Comments