Laode Muhammad Al Fatih
Laode Muhammad Al Fatih

Reputation: 4610

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.

Upvotes: 9

Views: 1492

Answers (1)

RobG
RobG

Reputation: 147453

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

Upvotes: 9

Related Questions