Determine start and end of DST by locale - client side

Discussion in 'Javascript' started by mikepuglisi@gmail.com, Jul 18, 2007.

  1. Guest

    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.
     
    , Jul 18, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,911
    Guus Bosman
    Jul 25, 2004
  2. Rakesh
    Replies:
    2
    Views:
    2,004
    Roedy Green
    Apr 3, 2009
  3. david.karr
    Replies:
    34
    Views:
    2,046
    David Karr
    Apr 21, 2009
  4. Roedy Green

    DST Start date

    Roedy Green, Oct 3, 2009, in forum: Java
    Replies:
    11
    Views:
    920
    John B. Matthews
    Oct 6, 2009
  5. zade
    Replies:
    1
    Views:
    641
    James Kanze
    Mar 5, 2010
Loading...

Share This Page