Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens if Date.now () is greater than Number.MAX_SAFE_INTEGER?

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.

like image 506
Laode Muhammad Al Fatih Avatar asked May 21 '20 23:05

Laode Muhammad Al Fatih


1 Answers

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
like image 115
RobG Avatar answered Oct 07 '22 12:10

RobG