I am trying to determine the start and end dates of daylight savings
time (DST) according to the user's locale specified in their system. I
am so close, but I am off by one hour. Here in the Florida, DST starts
at Sun Mar 11 02:00:00 EST 2007, but my function returns Sun Mar 11
01:00:00 EST 2007. I commentted my code to describe how I am
accomplishing the goal.
[script]
function displayDSTdates(){
var rightNow = new Date();
var dJanuaryFirst= new Date(rightNow.getFullYear(), 0, 1, 0,
0, 0, 0); //start at jauary first
var cTemp = dJanuaryFirst.toGMTString(); //convert to GMT
string
var dJanuaryFirstGMT = new Date(cTemp.substring(0,
cTemp.lastIndexOf(" ")-1)); //get GMT date object
var nStandardGMToffset = ((dJanuaryFirst - dJanuaryFirstGMT) /
(1000 * 60 * 60)); // get GMT offset for January First
var bDSTstartDateFound = false;
var nHoursInYear = (365 * 24);// not accurate for leap year,
but shouldn't matter since a DST start / end date never falls on the
last day of the year.
for(var i=0;i<nHoursInYear;i++){// Loop through all 8760 hours
in a year
var dSampleDate = new Date(dJanuaryFirst); // create new
date object starting a Jan 1st
dSampleDate.setHours(i);// advance i hours
var cSampleTemp = dSampleDate.toGMTString();
var dSampleDateGMT = new Date(cSampleTemp.substring(0,
cSampleTemp.lastIndexOf(" ")-1));
var nCurrentGMToffset = ((dSampleDate - dSampleDateGMT) /
(1000 * 60 * 60)) // get the GMT for this new hour
if(nCurrentGMToffset != nStandardGMToffset && !
bDSTstartDateFound){ // if it is not the same as as the
dJanuaryFirstGMT offset, then we know that we are adjusting for DST
document.getElementById('spnDSTstartDate').innerHTML
= dSampleDateGMT
nStandardGMToffset = nCurrentGMToffset; //switch the
nStandardGMToffset to the DST GMT offset
bDSTstartDateFound = true; //set the boolean flag to
true
continue;
}else if(nCurrentGMToffset != nStandardGMToffset){ //this
portion will prove true when the GMT offset goes back.(when DST is
over)
document.getElementById('spnDSTendDate').innerHTML =
dSampleDateGMT
return;
}
}
//If the function makes it this far, there was no adjustment
to GMT offset.
document.getElementById('spnDSTstartDate').innerHTML = 'Your
locale does not observe Daylight Savings Time';
document.getElementById('spnDSTendDate').innerHTML = 'Your
locale does not observe Daylight Savings Time';
}
[/script]
HTML:
In your locale, Daylight Savings Time Starts At = <span
id="spnDSTstartDate"></span><br>
In your locale, Daylight Savings Time Ends At = <span
id="spnDSTendDate"></span>
[script]
displayDSTdates();
[/script]
This code may not work in the southern hemisphere at this time since
their seasons are reversed, but I will worry about that after I get
this off-by-one bug resolved.
Thanks,
Mike