2

I'm attempting to insert some information in an object into a database. The object is a Stripe object (they process payments). That doesn't really matter; what matters is how they provide the server the 'date' a payment is/was made. They provide it in milliseconds like this:

{
    "created": 1489458464
}

That's supposed to be the time in milliseconds since January 1, 1970, 00:00:00 UTC... but its missing the last 3 digits.

So when I want to convert this to a readable time, I simply add 3 zeros to the end like this:

var date = 1489458464000;
var date2 = new Date(date);
console.log(date2.toString());

//Output: Mon Mar 13 2017 22:27:44 GMT-0400 (Eastern Daylight Time)

This seems like an acceptable solution to this problem; the last 3 digits don't seem to change the date/time.

I just want to confirm whether this is okay, or if someone thinks I should be doing something else. Thanks.

MonkeyOnARock
  • 2,151
  • 7
  • 29
  • 53

2 Answers2

3

Per their documentation it is not actually supposed to be in milliseconds, because they are using a UNIX timestamp, which is expressed as seconds since 1/1/1970. I'd say the most elegant solution would be to take note of that and then just multiply by 1000 in the constructor:

var date2 = new Date(date * 1000) //because Unix timestamps are in seconds
aremay
  • 113
  • 6
0

No, that doesn't look right to me.

In fact, see here:

Convert UTC Epoch to local date with javascript - Stack Overflow: Convert UTC Epoch to local date with javascript

Community
  • 1
  • 1
lazarus
  • 371
  • 2
  • 10