2

I'm honestly not sure how to phrase this question. Basically open a JavaScript console (node, your browser or wherever) and try this:

Date(564018060878018050) // 'Fri Nov 23 2018 06:22:20 GMT-0800 (Pacific Standard Time)'
new Date(564018060878018050) // <-- Invalid Date

I have no idea why the first one works and the second one doesn't. Is there another way to parse. I'm trying to stay away from using a library for this.

4
  • The first one you are converting the date and the second you are instantiating it Commented Nov 23, 2018 at 14:41
  • Where is this value coming from? .Net ticks? Commented Nov 23, 2018 at 15:39
  • 1
    "Is there another way to parse", maybe, if you say what date it represents and provide the parse or conversion algorithm. E.g. new Date(564018060878018050*1e-6).toISOString() produces "1987-11-15T23:41:00.878Z" so maybe it's nanoseconds from an epoch of 2000-01-01 (which would make it 2017-11-15)? Commented Nov 23, 2018 at 22:59
  • Yes I found out that it is in nanoseconds, thank you! I ended up changing it to seconds at the query level so it could be parsed in JavaScript. After searching briefly online it doesn't appear JavaScript supports nanosecond precision. Commented Nov 27, 2018 at 13:20

2 Answers 2

5

The specs says that:

The actual range of times supported by ECMAScript Date objects is [...] exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC.

The valid range is much smaller than the value you used (564,018,060,878,018,050).

And deep inside the Date(value) constructor we have:

If abs(time) > 8.64 × 1015, return NaN.

This explains why new Date(564018060878018050) yields invalid date.

As for Date(564018060878018050) the specs say that:

... Invoking a constructor without using new has consequences that depend on the constructor. For example, Date() produces a string representation of the current date and time rather than an object.

So Date(value) is supposed to return current date as a string and not a date.

> Date(564018060878018050) === (new Date()).toString()
< true

> typeof Date(564018060878018050)
< "string"
Sign up to request clarification or add additional context in comments.

Comments

0

You are calling the Date constructor as Function and as say in ECMAscript doc:

"When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC)."

"NOTE The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments."

You can find more details here: https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.