Re: Realtime clock that understands DST



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>
[/html]
[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


.



Relevant Pages

  • Determine start and end of DST by locale - client side
    ... came across some code which determines if the user's locale observes ... daylight savings time (DST) according to the user's locale specified ... var rightNow = new Date; ... locale does not observe Daylight Savings Time'; ...
    (comp.lang.javascript)
  • Re: Daylight Savings Time in Calendar
    ... In my Locale there is Daylight Savings Time (DST) in effect. ... home dot woh dot rr dot com slash jbmatthews ...
    (comp.lang.java.programmer)
  • Re: Did anybody ever patch GS/OSs auto daylight savings?
    ... President Bush signed the Energy bill few years ago. ... doesn't appear to have a way to turn the auto daylight savings time ... implemented vital changes that standardized the application of DST ...
    (comp.sys.apple2)
  • Re: Daylight Savings for Arizona
    ... Seems there was a preference when you installed the OS to select if you ... issue of what to do for users that do not follow DST. ... I have just posted an article "Is your Mac ready for Daylight Savings Time?" ... One of the top five MS Entourage resources listed on the Entourage Blog. ...
    (microsoft.public.mac.office.entourage)
  • Re: WARNING: AMD64 Daylight saving time issue.
    ... >> As a US resident subject to daylight savings, ... our state legislature voted to put us on DST starting next ... to Central time -- they could switch to Central as long as they were ... adjacent to another Central time county. ...
    (comp.unix.solaris)