Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What range of dates are permitted in Javascript?

What is the maximum and the minimum date that I can use with the Date object in Javascript?

Is it possible to represent ancient historical dates (like January 1, 2,500 B.C.) or dates that are far into the future (like October 7, 10,000)?

If these far-from-present dates can't be represented with the Date object, how should I represent them?

like image 854
Peter Olson Avatar asked Sep 30 '12 23:09

Peter Olson


1 Answers

According to §15.9.1.1 of the ECMA-262 specification,

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC.
...
The actual range of times supported by ECMAScript Date objects is ... exactly –100,000,000 days to 100,000,000 days measured relative to midnight at the beginning of 01 January, 1970 UTC. This gives a range of 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC.

So the earliest date representable with the Date object is fairly far beyond known human history:

new Date(-8640000000000000).toUTCString()
// Tue, 20 Apr 271,822 B.C. 00:00:00 UTC

The latest date is sufficient to last beyond Y10K and even beyond Y100K, but will need to be reworked a few hundred years before Y276K.

new Date(8640000000000000).toUTCString()
// Sat, 13 Sep 275,760 00:00:00 UTC

Any date outside of this range will return Invalid Date.

new Date(8640000000000001)   // Invalid Date
new Date(-8640000000000001)  // Invalid Date

In short, the JavaScript Date type will be sufficient for measuring time to millisecond precision within approximately 285,616 years before or after January 1, 1970. The dates posted in the question are very comfortably inside of this range.

like image 110
Peter Olson Avatar answered Oct 05 '22 23:10

Peter Olson