0

I have the following data structure. The first column is intervals. The first row of the interval datum is a unix time and the subsequent data are intervals (i.e. 300*1, 300*2, ect). The other column is the data values. Here is the head of the data:

a1521207300,555.45
1,554.53
2,554.07
3,553.9
4,552.67

And here I went about converting the unix time to a Date object. The a here is ornamental, so I slice() at 1 like so:

    var rawTime = data[0].interval;
    var timeValue = Math.round(rawTime.slice(1));
    console.log(timeValue)
    console.log(new Date(timeValue))

I also tried using parseInt() instead of round(). The console shows that this unix time is equivalent to: Jan 18 1970 which I had quite the guffaw at. Then I got to thinking, maybe I did something wrong. It's supposed to be a very recent date -- March 16th 2018. This is strange because my understanding is that javascript can be passed a unix date directly as per this answer.

I also checked the unix time at a conversion site: www.onlineconversion.com/unix_time.htm

Which confirmed that it's indeed a March 16th 2018 timestamp.

Question: Why is this unix date for my March 2018 data being treated like a 1970's date? Maybe the a is actually doing something after all... Anyway, what is the correct way to handle this time stamp? It's only 10 numerical digits, it does not seem to be a precision problem. Date can handle unix times up to 13 digits I believe.

Arash Howaida
  • 2,575
  • 2
  • 19
  • 50
  • January 18, 1970 would be a number like 1483200. Any idea how that number could have crept in? – Steve Summit Mar 18 '18 at 15:47
  • @SteveSummit Actually no, I haven't seen a number like that. It's a pretty simple program I have going. I've triple checked things for what it's worth. Maybe my IDE is bugged or something. – Arash Howaida Mar 18 '18 at 15:50
  • When I take away `new` from `new Date` it seems to be ok. Not sure why though. – Arash Howaida Mar 18 '18 at 15:54

1 Answers1

1

As per the documentation, when you invoke new Date(value) with an integer value, it is used as the number of milliseconds since January 1, 1970. To get the date you want, the value 1521207300 appears to be number of seconds instead of milliseconds. That is, you missed a factor of 1000. new Date(1521207300000) gives Fri Mar 16 2018.

When I take away new from new Date it seems to be ok. Not sure why though.

The documentation mentions the different behavior:

Note: JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.

It seems when called as a function Date(value), it treats the value as the number of seconds, instead of milliseconds. I didn't dig deep enough to confirm this, because it doesn't matter: the documentation says to not use it this way (and since it gives a string instead of a date object, it's not very useful anyway).

janos
  • 120,954
  • 29
  • 226
  • 236