homepage Welcome to WebmasterWorld Guest from 54.197.65.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Timezone with Daylight Savings Detection
username

5+ Year Member



 
Msg#: 4330375 posted 6:42 am on Jun 24, 2011 (gmt 0)

Hi all, I have been searching for a reliable script that calculates a users timezone offset, i.e GMT + 10. I want the script to also handle daylight savings periods also.

I found this script which seems to work, but I am not currently in DST, so am wondering if anyone is, or has a bullet proof script for calculating this that also handles half hours etc?

<script type="text/javascript">
function checkTimeZone() {
var rightNow = new Date();
var date1 = new Date(rightNow.getFullYear(), 5, 1, 0, 0, 0, 0);
var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
var temp = date1.toGMTString();
var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var temp = date2.toGMTString();
var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
if (hoursDiffDaylightTime == hoursDiffStdTime) {
document.write("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is NOT observed here.");
} else {
document.write("Time zone is GMT " + hoursDiffStdTime + ".\nDaylight Saving Time is observed here.");
}
}
</script>

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4330375 posted 6:49 am on Jun 24, 2011 (gmt 0)

One note. Use UTC in place of GMT. GMT was deprecated in 1971.

username

5+ Year Member



 
Msg#: 4330375 posted 7:29 am on Jun 24, 2011 (gmt 0)

Ok, cool. So obviously this script has some issues. Does any have a script using UTC that handles DST and returns the offset. i.e. 10 hours

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4330375 posted 7:36 am on Jun 24, 2011 (gmt 0)

It's just a text change in the visual printed messages.

The variable name inside PHP is still called GMTstring.

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4330375 posted 9:07 am on Jun 24, 2011 (gmt 0)

...Use UTC in place of GMT. ...The variable name inside PHP is still called GMTstring.


In JavaScript, the toGMTString() method is deprecated in favour of toUTCString() [w3schools.com]. The output, however, still contains the text 'GMT'.

JavaScript Date Object [w3schools.com]

penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4330375 posted 4:34 pm on Jun 24, 2011 (gmt 0)

Your script doesn't work for me. It correctly picks up the timezone offset (but that's the easy part... Date.timeZoneOffset() would do the same), but it states that DST is "NOT observed here", but it is (and I'm currently on DST here in the UK).

I think the script should state "DST is observed here" if your region switches to DST at some point, not whether you are currently on DST or not.

HOWEVER, and this is the problem, the script assumes DST switches at some point between June 1 and July 1 (date1 and date2 respt). What region is that? In the UK DST starts at the end of March. The script needs to test a date on either side of the switch, apart from that the dates really don't matter (I don't think).

If I change:
var date1 = new Date(rightNow.getFullYear(), 5, 1, 0, 0, 0, 0);


To
var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); // 0 - January


This then works for me. I think it will also work for the original region. Does it work for everyone? With the dates 6 months apart, I think it stands a good chance. But elsewhere in the world DST could start at anytime and last for longer than 6 months?! In the UK DST lasts for 7 months.

With these last points in mind I have my doubts whether you can write a compact JavaScript function that is able to test DST that will work worldwide, without looking up some local knowledge? How variable is DST around the world?

Actually, I wonder... if you know the timezone offset, can you make an intelligent assumption as to when the DST might be? eg. If offset is 0 or 1 then DST changes in March, then work backwards... If offset is -1, assume DST changes in February... etc?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4330375 posted 9:08 pm on Jun 24, 2011 (gmt 0)

DST implementation is very variable around the world.

When the US changes over there is a one-hour delay between each time zone. A similar effect is seen in Europe on a completely different date.

The dates around the world span several months.

More importantly the Southern hemisphere uses DST from roughly September to March while the Northern hemisphere uses it from roughly March to September.

Many places use DST for 7 months at a time.

The changes for the last half of 2011 are listed here: [timeanddate.com...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved