0

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..

Minutia
  • 111
  • 6

2 Answers2

1
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?

Jé Queue
  • 10,359
  • 13
  • 53
  • 61
  • I am running this in the latest version of chrome. – Minutia Oct 28 '19 at 15:28
  • thanks for confirming. It looks like the time I was parsing was still string. Using `parseInt(time)` fixed the issue. Does muliplying a var by 1000 implicity convert the variable to an integer in Javascript? – Minutia Oct 28 '19 at 19:21
0

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.

ben
  • 58
  • 5