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.
9 Comments to Javascript: Figuring out daylight savings time.
Leave a comment
Search
Recent
Topics
Comments
- Thank you, kind person. You have saved me a good deal of time. -- Adam Williams
IE8 Javascript external s[..] - Thanks for this code. I am using it with some modification to determine DST for any year, not just 2009. This has save -- Jonathan
Javascript: Figuring out [..] - I do not handle the 2AM switch over. This would be a minor bug. I checked your solution that would work I have never l -- ben
Javascript: Figuring out [..] - Your also doesn't seem to take into account the hour of the day. Daylight savings switch happens at 2AM... So there is -- Kevin
Javascript: Figuring out [..] - Matt corrected code has been posted. -- ben
Javascript: Figuring out [..]
















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.
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.
Code updated and fixed.
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.
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.
Matt corrected code has been posted.
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.
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
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