Search code examples
javascriptdatetimemomentjsunix-timestamp

Can't convert unix epoch / timestamp to proper time


I am getting timestamps for estimated bus arrival times from an API as a timestamp / epoch: 1536589019000. If I go to a website like this I get the appropriate format:

Monday, September 10, 2018 7:16:59 AM

But if I attempt to convert the date in javascript, for example, with momentjs, I get some date far into the future: 50662-08-08 08:03

moment.unix(estimatedArrivalTime).format("YYYY-MM-DD HH:mm")

How do I convert a unix timestamp properly?


Solution

  • You're getting a timestamp in millesconds instead of seconds. Just passing it like so should work: moment(1536589019000).format("YYYY-MM-DD HH:mm")