11

In JavaScript, what is the maximum value for the year in a Date?

How can I find this out in code?

I have tried the following:

new Date().getFullYear().MAX_VALUE;

Thanks in advance.

Simon
  • 7,991
  • 21
  • 83
  • 163

1 Answers1

23

As per specs here: https://262.ecma-international.org/11.0/#sec-time-values-and-time-range : The actual range of times is 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. So, the maximum valid year you will get is 275760 (new Date(8640000000000000).getFullYear()). And to get the minimum valid year, new Date(-8640000000000000).getFullYear() or 271821 B.C.E.

rgajrawala
  • 2,148
  • 1
  • 22
  • 35
  • So ... ... I should have a `try... catch` on dates so that my code is ready for the September 12, 275760 bug. Got it. – Yanick Rochon Aug 10 '22 at 13:34
  • @YanickRochon The call to `Date().getFullYear()` shouldn't throw an error but instead return `NaN`. So it depends on how your code handles that. – rgajrawala Aug 11 '22 at 17:53