Your problem is you're missing the new
keyword.
Date() // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
Date(1) // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
Date('something random') // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
However, with new
:
new Date() // Mon Oct 15 2018 13:30:55 GMT+1000 (Australian Eastern Standard Time)
new Date(1) // Thu Jan 01 1970 10:00:00 GMT+1000 (Australian Eastern Standard Time)
new Date('something random') // Invalid Date
See the note on MDN:
calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object
When you call Date()
without new
, it calls a function that returns a string of the current time. It takes no arguments, but in Javascript, you can pass whatever you like to it and it will still work, so it just ignores any arguments that you pass, and returns a string
, and not a Date of the current time.
typeof Date() // string
typeof new Date() // object
As noted by edvilme, your timestamps also look to be in seconds, not milliseconds as required by new Date()
- you can just multiply by 1000 to make it work
new Date(1529491531) // Mon Jan 19 1970 02:51:31 GMT+1000 (Australian Eastern Standard Time)
new Date(1529491531 * 1000) // Wed Jun 20 2018 20:45:31 GMT+1000 (Australian Eastern Standard Time)