7

Of course it took another 200,000 years for that to happen. But will the Javascript dating system error after the value of Date.now() exceeds the value of Number.MAX_SAFE_INTEGER? What consequences will occur?

Maybe this question looks strange and useless. But can anyone answer my curiosity and also other people who might have the same question.

Laode Muhammad Al Fatih
  • 3,994
  • 1
  • 18
  • 32
  • 2
    Epoch time returned from `Date.now` simply counts the number of seconds since Jan 1, 1970. Have you ever wondered how to represent dates _before_ that time? Forget about 200,000 years into the future, that's just 50 years ago! Using an integer to represent a date is convenient, but it's not an implementation requirement. You have a long time to come up with another Date standard that supports a wider range of dates. – Mulan May 21 '20 at 23:09
  • 5
    I've come from the year 202020 - javascript is no longer used, since we don't have web pages any more, we've evolved beyond such primitive forms of information transfer - all information is passed using odours – Jaromanda X May 21 '20 at 23:09
  • @JaromandaX Hahahaha – Squidward Tentacles May 21 '20 at 23:14
  • The computer explodes, be careful. – virtual_Black_Whale May 23 '20 at 00:35

1 Answers1

7

What you describe is ruled out by ECMA-262. The maximum value that can be returned by Date.now is ±8.64e15, which is well within the range of integers safely supported by ECMAScript.

The maximum value can represent 1e9 days either side of the epoch (1 Jan 1970) so a range of approximately ±273,790 years. I think there will be time to address the issue before it arises.

Constructing a date for the maximum value returns a date for +275760-09-13T00:00:00.000Z. Adding one millisecond to the time value returns an invalid date:

// Max value returnable by Date.now
let maxDateNowValue = 8.64e15;

console.log(new Date(maxDateNowValue).toISOString()); // +275760-09-13T00:00:00.000Z

// Max value plus 1 millisecond
let plusOneMS = maxDateNowValue + 1;

console.log(new Date(plusOneMS).toString()); // Invalid Date
RobG
  • 142,382
  • 31
  • 172
  • 209