How to convert UTC date and time to EST date and time

I use the following code to get the current UTC time in the correct format, but the client came back and asked for the timestamp to be in EST instead of UTC. I searched google and stackoverflow but cannot 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 constant timestamp for EST regardless of where the browser is in the world, therefore, UTC is originally used.

Thanks!

+4
source share
2 answers

A few things...

  • "EST" , . , .

  • EST . , . Eastern Daylight Time, EDT. , " ", .

  • - IANA. wiki. , " " , "/-".

  • JavaScript , UTC. (, bjb568, .) , , .

  • . , . , , UTC.

  • , moment.js - . :

    moment().tz("America/New_York").format("YYYY-MM-DD HH:mm:ss")
    
+5

( ) . ( , UTC) "2014-10-09T20: 30: 54Z" .

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

javascript Date() .

:

var tmpDate = new Date("Fri Jul 21 02:00:00 GMT 2012")
alert(tmpDate);
//Result: Fri Jul 20 22:00:00 EDT 2012
+2

All Articles