admin管理员组

文章数量:1426066

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.

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.

Share Improve this question asked May 21, 2020 at 23:04 Laode Muhammad Al FatihLaode Muhammad Al Fatih 4,6401 gold badge22 silver badges38 bronze badges 4
  • 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 e up with another Date standard that supports a wider range of dates. – Mulan Commented May 21, 2020 at 23:09
  • 7 I've e 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 Commented May 21, 2020 at 23:09
  • @JaromandaX Hahahaha – Squidward Tentacles Commented May 21, 2020 at 23:14
  • The puter explodes, be careful. – virtual_Black_Whale Commented May 23, 2020 at 0:35
Add a ment  | 

1 Answer 1

Reset to default 9

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

本文标签: javascriptWhat happens if Datenow () is greater than NumberMAXSAFEINTEGERStack Overflow