Determine start and end of DST by locale - client side

M

mikepuglisi

I am working on the same exact thing using only client-side code. I
came across some code which determines if the user's locale observes
DST. I adapted the concept 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.

<head>
<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>
</head>
<body>
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>
</body>
</html>

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. You can test be changing your system
timezone. Arizona is a goood test, since they don't observe DST.


Please help to get this solved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top