8

I'm using the following code to get the current UTC time in the correct format, but the client has come back and asked that the timestamp now be in EST instead of UTC. I have searched Google and stackoverflow, but can't find an answer that works with my existing code.

var currentdate = new Date(); 
var datetime = currentdate.getFullYear() + ":"
            + ("0" + (currentdate.getUTCMonth()+1)).slice(-2) + ":"
            + ("0" + currentdate.getUTCDate()).slice(-2) + ":"
            + ("0" + currentdate.getUTCHours()).slice(-2) + ":"  
            + ("0" + currentdate.getUTCMinutes()).slice(-2) + ":" 
            + ("0" + currentdate.getUTCSeconds()).slice(-2);

What we are trying to do is set a consistent timestamp to EST regardless of where the browser is located in the world, hence the use of the UTC time originally.

Thanks!

Andrew R
  • 83
  • 1
  • 1
  • 3
  • 2
    possible duplicate of [How to convert datetime from the users timezone to EST in javascript](http://stackoverflow.com/questions/9070604/how-to-convert-datetime-from-the-users-timezone-to-est-in-javascript) – bjb568 Jan 07 '14 at 07:20
  • Yes, except that link does not handle daylight saving time properly. – Matt Johnson-Pint Jan 07 '14 at 16:03

3 Answers3

15

A few things...

  • The term "EST" can be used for either Eastern Standard Time in North America, or Eastern Standard Time in Australia. Most time zone abbreviations are not unique, so you should be more specific.

  • EST also does not define a time zone in its entirety. It only defines part of the time zone that is used during the winter months. The other half is called Eastern Daylight Time, abbreviated EDT. Your client probably meant "Eastern Time", which would need to take both into account.

  • The typical way to define time zones is as an identifier from the IANA time zone database. You can read more about it in the timezone tag wiki. For example, if your client meant Eastern Time in the United States, then your time zone is "America/New_York".

  • JavaScript inherently doesn't know anything about time zones, other than it's own and UTC. (The link that bjb568 gave does not handle daylight saving time properly.) In order to work with them on the client, you will need to use one of the libraries I list here.

  • Your current code is a bit strange in terms of output. Usually colons are used for separating only the time, and you are using them for all parts. Anyway, it's not converting anything, it's just outputting UTC.

  • You might do well with a library like moment.js and it's moment-timezone add-on. For example:

    moment().tz("America/New_York").format("YYYY-MM-DD HH:mm:ss")
    
Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Matt, the client wants U.S. EST (Boston, MA), without any changes for Daylight Savings Time. This is why I didn't use any of the libraries that account for DST. As for the formatting, this is being injected into an analytics application, so the format is very specific and required to be the way that I have it here. – Andrew R Jan 07 '14 at 16:18
  • 1
    Just a suggestion, but perhaps you should verify the requirements. Boston does indeed follow daylight saving time. The only scenario that I know of where DST is intentionally ignored is in the financial sector (stock trading), and only then in particular scenarios. – Matt Johnson-Pint Jan 07 '14 at 16:46
4

The following answer (copied from this link on this site) worked very well for me. It easily and correctly converted the following Zulu time (which is same as UTC) "2014-10-09T20:30:54Z" to South African time.

var tmpDate = New Date("enter any valid Date format here")

The javascript Date() function will automatically convert it to your local time.

Example:

var tmpDate = new Date("Fri Jul 21 02:00:00 GMT 2012")
alert(tmpDate);
//Result: Fri Jul 20 22:00:00 EDT 2012
Community
  • 1
  • 1
AxMann
  • 41
  • 1
0

Below line will work:

console.log(moment().tz("America/New_York").format("YYYY-MM-DD HH:mm:ss"))
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Sabita Nadar
  • 51
  • 1
  • 7