0

I have found this piece of code in a project I am working on:

+new Date(DateTime)

Note the + before new. If I pass in this value:

'2020-09-15T01:00:00Z'

It outputs this value:

1600131600000

Can anyone explain to me what this value is? It is not unix time - or at least the unix time moment outputs:

moment().unix(DateTime) = 1531737560

I am cleaning up my date formats within the project and cannot work out how to recreate this with moment...

EDIT:

How can I recreate this in moment.js?

Alex
  • 3,730
  • 9
  • 43
  • 94
  • 1
    https://stackoverflow.com/questions/221539/what-does-the-plus-sign-do-in-new-date – ADyson Jul 16 '18 at 10:52
  • P.S. your momentJS code makes no sense. Try `moment(DateTime).unix()`. You want to _output_ the unix timestamp, not try and _parse_ `DateTime` as if it were already a timestamp. See http://momentjs.com/docs/#/displaying/unix-timestamp-milliseconds/ And even if it _were_ a timestamp you were trying to parse, the code would in any case be moment.unix(timestamp) (as per the docs http://momentjs.com/docs/#/parsing/unix-timestamp-milliseconds/). So what you wrote is incorrect in both interpretations. Check the docs more carefully. – ADyson Jul 16 '18 at 10:58
  • *"It is not unix time..."* It is, it's just in milliseconds rather than seconds. *"How can I recreate this in moment.js?"* Probably the simplest way is to get the JavaScript date from it, and use its `getTime`: `yourMomentInstance.toDate().getTime()` (or `+yourMomentInstance.toDate()`). If you want "now" (what `+new Date()` does), use `Date.now` or `moment.now`. – T.J. Crowder Jul 16 '18 at 11:02
  • @T.J.Crowder if you write that as the answer I will accept it – Alex Jul 16 '18 at 11:05
  • 1
    @Alex there's a much simpler way than that to get the equivalent output...momentJS provides .unix() and .valueOf() methods depending on if you want seconds or milliseconds. See my comment above (although I forgot about the valueOf() bit). See also http://jsfiddle.net/svqrxpt0/5/ P.S. your question has been marked duplicate (based on the original edit, I assume) so no answers can be posted unless it's voted to be re-opened – ADyson Jul 16 '18 at 11:06
  • 1
    @ADyson - Ah, good, I was wondering why I couldn't find a "milliseconds since The Epoch" accessor in Moment! `valueOf`. Good to know. – T.J. Crowder Jul 16 '18 at 12:49

0 Answers0