Minutia
Minutia

Reputation: 121

Issues With Converting Epoch Time to Correct Date

I have files with epoch time stamps such as 1564002293050. Using https://www.epochconverter.com/ this shows Wednesday, July 24, 2019 9:04:53.050 PM however my code shows Mon Apr 06 51531 02:24:10 GMT-0700 (Pacific Daylight Time). Why is this?

Because the times were generated in Unix, I multiplied by 1000 for ms. This value is then displayed.

time = 1564002293050;
var dateStamp = new Date(time* 1000);

Edit: Ive referenced this post and several similar others. It is good to note that not multiplying it by 1000 will result with "Invalid Date".

Edit 2: Figure it out. I was parsing the data but looks like I had to convert it to an integer parseInt(time) ended up fixing the issue. Sorry for the unrelated solution..

Upvotes: 0

Views: 828

Answers (2)

Jé Queue
Jé Queue

Reputation: 10637

document.write(new Date(1564002293050));

Prints Wed Jul 24 2019 15:04:53 GMT-0600 (Mountain Daylight Time) (in my TZ).

Even this outputs the same in HTML, despite not syntactically declaring said variable.

time = 1564002293050;
var dateStamp = new Date(time);
document.write(dateStamp);

Are you doing this on the browser? Another Javascript engine?

Upvotes: 1

ben
ben

Reputation: 58

The Date constructor takes the epoch time in milliseconds (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date). Your time is in milliseconds. No need to multiply it by 1000.

Upvotes: 0

Related Questions