In the USA, DST is currently calculated as the time falling between the 2nd Sunday in March and the 1st Sunday in November.
To calculate the date DST begins, we first calculate the current year, then what weekday the first of March fall on, and based on that we can calculate the 2nd Sunday. The same is done for November, and finally a test of whether today's date falls between the two anchor dates. dst is turned to "1" when true.
For my needs day-level precision is adequate, but it could be built out to greater precision.
var dst = 0
var d = new Date();
var myYear = parseInt(d.getFullYear())
d.setFullYear(myYear , 2, 1);
var what_Weekday_Mar1 = d.getDay();
var DST_StartDay
switch(what_Weekday_Mar1) {
case 0:
DST_StartDay = 14
break;
case 1:
DST_StartDay = 13
break;
case 2:
DST_StartDay = 12
break;
case 3:
DST_StartDay = 11
break;
case 4:
DST_StartDay = 10
break;
case 5:
DST_StartDay = 9
break;
case 6:
DST_StartDay = 8
break;
}
var DST_on_Date = new Date()
DST_on_Date = DST_on_Date.setFullYear(myYear,2,DST_StartDay)
d.setFullYear(myYear , 10, 1);
var what_Weekday_Nov1 = d.getDay();
switch(what_Weekday_Nov1) {
case 0:
DST_EndDay = 1
break;
case 1:
DST_EndDay = 7
break;
case 2:
DST_EndDay = 6
break;
case 3:
DST_EndDay = 5
break;
case 4:
DST_EndDay = 4
break;
case 5:
DST_EndDay = 3
break;
case 6:
DST_EndDay = 2
break;
}
var DST_off_Date = new Date()
DST_off_Date = DST_off_Date.setFullYear(myYear,10,DST_EndDay)
var toDay = new Date();
/*if today is between Daylight Savings On and Off times */
if (today>DST_on_Date && today<DST_off_Date) {
dst = 1;
}
hope this helps!
-RL