Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS wrong unix time parse result

I have a timestamp 1378028575 that gives me Sun, 01 Sep 2013 09:42:55 GMT here. But when I try to format it with Angular date, it returns it as Jan 17, 1970 2:47:08 AM, using this format: {{'1378028575' | date:'medium'}}. The result from the site is correct but in Angular is wrong. Why does it happen, or what am I doing wrong?

like image 206
Sergei Basharov Avatar asked Sep 09 '13 10:09

Sergei Basharov


2 Answers

Its cause you use seconds not milliseconds.

new Date(1378028575)
Fri Jan 16 1970 23:47:08 GMT+0100 (CET)

new Date(1378028575000)
Sun Sep 01 2013 11:42:55 GMT+0200 (CEST)

from the angular docs:

Date to format either as Date object, milliseconds (string or number) or various ISO 8601 datetime string formats (e.g. yyyy-MM-ddTHH:mm:ss.SSSZ and its shorter versions like yyyy-MM-ddTHH:mmZ, yyyy-MM-dd or yyyyMMddTHHmmssZ). If no timezone is specified in the string input, the time is considered to be in the local timezone.

like image 154
Andreas Köberle Avatar answered Nov 15 '22 13:11

Andreas Köberle


The other answer isnt quite complete. Since your timestamp is in seconds, not miliseconds, in Angular.js you can do this:

{{1378028575 * 1000 | date:'medium'}}

Knowing seconds * 1000 = miliseconds is one thing. Knowing you can put math in the date expression is another :)

like image 30
Jordy Kirkman Avatar answered Nov 15 '22 15:11

Jordy Kirkman