Created
April 28, 2010 18:43
-
-
Save kares/382528 to your computer and use it in GitHub Desktop.
same as Date.getTimezoneOffset() but with DST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculate the time zone offset with DST ! | |
* e.g. Prague - if DST enabled ? -60 (summer) : -120 (winter) | |
* | |
* NOTE: this is to be compatible to how Rails helper | |
* time_zone_options_for_select renders zones e.g. : | |
* - in summer (GMT+01:00) Prague | |
* - in winter (GMT+02:00) Prague | |
*/ | |
function getTimeZoneOffsetDST() { | |
// NOTE: return new Date().getTimezoneOffset() is not enought ! | |
var today = new Date(); | |
// 2nd Sunday in March can't occur after the 14th : | |
var dstBeg = new Date("March 14, "+ today.getFullYear() +" 02:00:00"); | |
// 1st Sunday in November can't occur after the 7th : | |
var dstEnd = new Date("November 07, "+ today.getFullYear() +" 02:00:00"); | |
dstBeg.setDate( 14 - dstBeg.getDay() ); // Calculate second Sunday in March | |
dstEnd.setDate( 7 - dstEnd.getDay() ); // Calculate first Sunday in November | |
if ( today >= dstBeg && today < dstEnd ) { // isDST | |
// e.g. for GMT+02:00 returns -120 ! | |
return today.getTimezoneOffset() + 60; | |
} | |
else { | |
return today.getTimezoneOffset(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment