Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Date() giving wrong value

The Unix epoch time 1354341600 translates to GMT: Sat, 01 Dec 2012 06:00:00 GMT (via http://www.epochconverter.com/).

Doing new Date(1354341600) on Javascript Fri Jan 16 1970 10:12:21 GMT-0600 (CST) (I'm in Illinois)

Relevant JSFiddle: http://jsfiddle.net/DAQkZ/

How do I get the correct Date/time in Javascript?

like image 302
Narabhut Avatar asked Jan 29 '26 12:01

Narabhut


2 Answers

Gotta multiply by 1000... to get milliseconds since Jan 1, 1970

alert(new Date(1354341600*1000));
like image 112
Dimitri Avatar answered Jan 31 '26 02:01

Dimitri


Your issue is with the units you're using. The "Time from epoch" is in seconds, and the JS Date is expecting Milliseconds.

Try this:

alert(new Date(1354341600000));
like image 24
Valor Avatar answered Jan 31 '26 02:01

Valor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!