0

I'm working with a service that gives me broadcast times for Television shows in Unix Time (seconds since midnight, January 1st, 1970, in Greenwich, England). I need to convert this, in javascript, to Eastern Standard time (USA). I need to account for daylight savings time, and for the fact that the client's clock may be set to something other than Eastern Standard time. I'm sure this code has been written before. Can anyone point me toward it?

morgancodes
  • 25,055
  • 38
  • 135
  • 187

3 Answers3

3

What you'll find is it's not possible to translate to a specific timezone, but as long as your users are in the desired timezone, this will work:

var date = new Date();
date.setTime(unixTime * 1000);

The resulting date object will display in the timezone of the computer running the browser:

window.console.log(date.toString())

yields:

"Thu Jun 25 2009 09:48:53 GMT-0400 (EDT)" 

for me anyway)

Mark Renouf
  • 30,697
  • 19
  • 94
  • 123
  • Thanks for the response, but whatever my solution is, it's going to need to subtract four hours from the unix time, and is going to then need to handle daylight savings. Don't think what you posted will do that. – morgancodes Jun 25 '09 at 13:49
  • This all relies on the timezone information of the browser. If the system has the correct tzdata, then it will. You can test this by creating Dates for summer vs. winter times (assuming your local TZ observes DST), and using the getTimezoneOffset() method of Date. – Mark Renouf Jun 25 '09 at 13:54
  • I was figuring I could bet back to GMT by subtracting the timeZoneOffset, then manually subtracting four hours, and manually correcting for daylight savings. A little ugly, but seems to me that would work no matter what time zone the browser thinks it's in. – morgancodes Jun 25 '09 at 13:59
  • Mark's solution is correct. The timestamp is a unique point in time @ GMT, the browser will (with correct client settings of course) adjust the time for however it should be displayed on the client. Attempting to adjust the date will just send you out on deep water. – Svend Jun 25 '09 at 14:04
  • I appreciate the responses, and totally believe that Mark's solution is best if the user is in the correct time zone. However, if the user isn't, I still want to be able show the time in Eastern Standard time, rather than whatever time zone the user is in. Seems it may be a bit ugly to do that though. – morgancodes Jun 25 '09 at 15:02
  • The problem of manually doing this is subtracting four hours will break between September and March (when EDT becomes EST and the TZ offset goes back to -5). And then these transition dates change (as they did back in 2006 I believe) – Mark Renouf Jun 25 '09 at 18:00
0

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6016329.html

Looks to have a solution for changing timezones, but it does look like you have to do the math yourself. There is no, setTimezone or setLocale method.

Community
  • 1
  • 1
jon077
  • 10,303
  • 11
  • 39
  • 37
0

I wrote some code which will turn GMT milliseconds into a Date-like object which can be queried for eastern standard time values. It handles daylight savings.

ESTDate = function(millis){

    if(isNaN(parseInt(millis))) {
        throw new Error("ESTDate must be built using a number");
    }

    var MILLIS_PER_DAY = 1000 * 60 * 60 * 24;
    var gmtDate = new Date(millis);

    var clockSetDate = function(month){

        var date = new Date(0);
        date.setUTCFullYear(gmtDate.getUTCFullYear());
        date.setUTCMonth(month);
        date.setUTCHours(2);

        while(date.getUTCDay() != "0"){
            date.setTime(
                date.getTime() + MILLIS_PER_DAY
            );  
        };
        return date;
    }

    var startStandarTimeDate = clockSetDate(2);
    var endStandardTimeDate = clockSetDate(10);
    date = new Date(millis);
    var estOffset = 60 * 60 * 1000 * 4;
    var dltOffset = (
            (startStandarTimeDate < date) && 
            (date < endStandardTimeDate)
        ) ? 0: 60 * 60 * 1000;
    date.setTime(date.getTime() - (estOffset + dltOffset));
    var self = { 

        getDate: function(){
            return date.getUTCDate();
        },
        getDay:function(){
            return date.getUTCDay();
        },
        getFullYear:function(){
            return date.getUTCFullYear();
        },
        getHours:function(){
            return date.getUTCHours();
        },
        getMilliseconds:function(){
            return date.getUTCMilliseconds();
        },
        getMinutes:function(){
            return date.getUTCMinutes();
        },
        getMonth:function(){
            return date.getUTCMonth();
        },
        getSeconds:function(){
            return date.getUTCSeconds();
        }
    }
    return self;
}
morgancodes
  • 25,055
  • 38
  • 135
  • 187
  • Rename: startStandarTimeDate to startStandardTimeDate. Have you changed the timezone to EST? If a user is in CST, new Date(millis) is in central time right? do you need to account for date.getTimezoneOffset() ? – jon077 Aug 17 '09 at 16:45