Dr said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!
Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]
There may be an error within an hour of the critical moment.
Seems to work OK (1987 and onward), but why make your code so difficult
to decipher?
Given the frequency of change and probable need for it to be modified
for a great variety dates, there is a strong need for code that is
portable and easily maintained. It should also handle date ranges that
span New Year.
The wikipedia daylight saving entry is:
<URL:
http://en.wikipedia.org/wiki/Daylight_Saving_Time>
The examples below should help.
/* Changeover to and from US DST occurs at 0200 hrs (2:00 am)
* on the following days:
*
* 1987 to 2006 inclusive:
* 1st Sunday in April to last Sunday in October
*
* 2007 onward (until next change...):
* 2nd Sunday in March to first Sunday in November
*
* Note that there have been many changes to the start days for
* DST - see:
*
http://aa.usno.navy.mil/faq/docs/daylight_time.html
*
http://www.energy.ca.gov/daylightsaving.html
*/
function DSTdatesUS( yr )
{
if ( yr<1987 ) return; // Outside range
// Work out last possible on/off date
var don = (yr<2007)? 38 : 14;
var dof = (yr<2007)? 31 : 38;
// Create initial on/off date objects
var DSTon = new Date(yr, 2, don, 2);
var DSToff = new Date(yr, 9, dof, 2);
// Adjust to Sunday before last possible date (adjustment may be 0)
DSTon.setDate( DSTon.getDate() - DSTon.getDay() );
DSToff.setDate( DSToff.getDate() - DSToff.getDay() );
return [DSTon, DSToff];
}
/* Australian Eastern Daylight Saving Time (AEDST) [on, off]
*
* Since 2001, the changeover to AEDST occurs at 0200 hrs (2:00 am)
* on the last Sunday in October and finishes at 0200 hrs on the
* last Sunday in March the following year.
*
* Note: Of the states 4 states in the AEST time zone, there are 2
* different ranges for daylight saving and one state that doesn't
* observe it at all. Start and end dates vary frequently.
*
* Legislation is before the NSW govt to extend the end of DST in 2006
* by one week.
*
* yr is the start year.
*/
function DSTdatesAEDST( yr )
{
if ( yr<2001 ) return; // Outside range
// Initially set to last possible on/off dates
var don = 31;
var dof = 31;
// If legislation passes, NSW DST extends by 7 days in 05/06
if (2005 == yr ) dof += 7;
// Create initial on/off date objects
var DSTon = new Date(yr, 9, don, 2);
var DSToff = new Date(yr, 14, dof, 2);
// Adjust to Sunday before last possible date (adjustment may be 0)
DSTon.setDate( DSTon.getDate() - DSTon.getDay() );
DSToff.setDate( DSToff.getDate() - DSToff.getDay() );
return [DSTon, DSToff];
}