Skip to content

Instantly share code, notes, and snippets.

@solomax
Created September 3, 2014 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solomax/e86cc77694d67120b8f5 to your computer and use it in GitHub Desktop.
Save solomax/e86cc77694d67120b8f5 to your computer and use it in GitHub Desktop.
JS code to get DST start date/time
function testTZ() {
//we assume here jan1 is not on the "DST edge"
var jan1 = new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0)
, offset = -jan1.getTimezoneOffset()
, dstPresent = false, dstMonth = 0, MINUTE_STEP = 5;
for (var i = 1; i < 12; ++i) {
var d = new Date(jan1.getFullYear(), i, 1, 0, 0, 0, 0), curOffset = -d.getTimezoneOffset();
if (curOffset != offset) {
dstPresent = true;
}
if (curOffset < offset) {
offset = curOffset; //Southern Hemisphere DST end is earlier than DST start
continue;
}
if (curOffset > offset) {
dstMonth = i - 1;
break;
}
}
document.write("dst ? " + dstPresent + ", month == " + dstMonth);
if (dstPresent) {
var dstDay = 1, dstHour = 0, dstMinute = 0
, day1 = new Date(jan1.getFullYear(), dstMonth, dstDay, dstHour, dstMinute, 0, 0)
, offset = -day1.getTimezoneOffset();
for (var i = 2; i < 32; ++i) {
var d = new Date(day1.getFullYear(), dstMonth, i, dstHour, dstMinute, 0, 0), curOffset = -d.getTimezoneOffset();
if (d.getMonth() != dstMonth) {
break; //move out of month, most probably impossible TODO check
}
if (curOffset > offset) {
dstDay = i - 1;
break;
}
}
day1 = new Date(jan1.getFullYear(), dstMonth, dstDay, dstHour, dstMinute, 0, 0);
for (var i = 1; i < 24; ++i) {
var d = new Date(day1.getFullYear(), dstMonth, dstDay, i, dstMinute, 0, 0), curOffset = -d.getTimezoneOffset();
if (curOffset > offset) {
dstHour = i - 1;
break;
}
}
day1 = new Date(jan1.getFullYear(), dstMonth, dstDay, dstHour, dstMinute, 0, 0);
for (var i = MINUTE_STEP; i < 60; i = i + MINUTE_STEP) {
var d = new Date(day1.getFullYear(), dstMonth, dstDay, dstHour, i, 0, 0), curOffset = -d.getTimezoneOffset();
if (curOffset > offset) {
dstMinute = i - MINUTE_STEP; //New Zealand - Chatham Islands has "DST minute" == 45
break;
}
}
document.write("; dstDay == " + dstDay + "; dstHour == " + dstHour + "; dstMinute == " + dstMinute);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment