0

Is there a simple (or otherwise good) way of getting the current date and time in Eastern time, allowing for possible daylight savings in the current and the target timezone? (America/New_York, I suppose, is the real target, for those using tz.)

The main difficulty, of course, is finding out what the current time zone is used in New York (EST or EDT). Can Javascript do this? Or are there good libraries for it? I'm wary of hard-coding the calculation because my code will fall out of date when new laws are passed (where libraries are more likely to be updated and languages still more).

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Charles
  • 11,269
  • 13
  • 67
  • 105
  • Do you want to show a specific time and not use the time from the user's system? – lmcanavals Mar 12 '12 at 14:52
  • You may find your answer at http://www.w3schools.com/js/js_obj_date.asp – Hope4You Mar 12 '12 at 14:53
  • @MartínCanaval: Yes, correct. For simplicity imagine that I'm displaying a dialog: "Current daylight-savings-adjusted time in New York is: _____". – Charles Mar 12 '12 at 16:05
  • @zaf: That topic is about the much easier question "How do I convert the user's current time to EDT?". Mine is harder, because it requires knowing when (say) New York is using EST and when EDT. – Charles Mar 12 '12 at 16:07
  • @Charles What about http://stackoverflow.com/questions/1194933/javascripts-date-gettimezoneoffset – zaf Mar 12 '12 at 16:54
  • @zaf: That gives me the current timezone, but not which one (EST or EDT) to convert to. – Charles Mar 12 '12 at 17:25

1 Answers1

1

(It is too much trouble to do this for only one timezone.) This returns the strings for US timezones with current dst rules.

You'll need the dst start and end rules for any zone you add

Date.toTZString= function(d, tzp){
    var short_months= ['Jan', 'Feb', 'Mar', 'Apr', 'May',
     'Jun', 'Jul','Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var h, m, pm= 'pm', off, label, str,
    d= d? new Date(d):new Date();

    var tz={
        AK:['Alaska', -540],
        A:['Atlantic', -240],
        C:['Central', -360],
        E:['Eastern', -300],
        HA:['Hawaii-Aleutian', -600],
        M:['Mountain', -420],
        N:['Newfoundland', -210],
        P:['Pacific', -480]
    }[tzp.toUpperCase()];

    //get the selected offset from the object:
    if(!tz) return d.toUTCString();
    off= tz[1];

    //get the start and end dates for dst:(these rules are US only)
    var     y= d.getUTCFullYear(), countstart= 8, countend= 1,
    dstart= new Date(Date.UTC(y, 2, 8, 2, 0, 0, 0)),
    dend= new Date(Date.UTC(y, 10, 1, 2, 0, 0, 0));
    while(dstart.getUTCDay()!== 0) dstart.setUTCDate(++countstart);
    while(dend.getUTCDay()!== 0) dend.setUTCDate(++countend);

    //get the GMT time for the localized dst start and end times:
    dstart.setUTCMinutes(off);
    dend.setUTCMinutes(off);

    // if the date passed in is between dst start and dst end, adjust the offset and label:
    if(dstart<= d && dend>= d){
        off+= 60;
        label= tzp+'dt';
    }
    else label= tzp+'st';

    //add the adjusted offset to the date and get the hours and minutes:
    d.setUTCMinutes(d.getUTCMinutes()+off);
    h= d.getUTCHours();
    m= d.getUTCMinutes();
    if(h> 12) h-= 12;
    else if(h!== 12) pm= 'am';
    if(h== 0) h= 12;
    if(m<10) m= '0'+m;

    //return a string:
    var str= short_months[d.getUTCMonth()]+' '+d.getUTCDate()+', ';
    return str+ h+':'+m+' '+pm+' '+label.toUpperCase();
}



//test1:
var d= new Date().toUTCString();
[d, Date.toTZString(d, 'E'), Date.toTZString(d, 'P')].join('\n');


Mon, 12 Mar 2012 17:46:30 GMT
Mar 12, 1:46 pm EDT
Mar 12, 10:46 am PDT

//test2:
var d=new Date(1352134800000).toUTCString();
[d,Date.toTZString(d, 'E'),Date.toTZString(d, 'P')].join('\n');


Mon, 05 Nov 2012 17:00:00 GMT
Nov 5, 12:00 pm EST
Nov 5, 9:00 am PST
kennebec
  • 102,654
  • 32
  • 106
  • 127