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?
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?
- So, what's the point of asking "If these far-from-present dates can't be represented..." when you already knew they could be represented? Why pose the question, if you know it's moot? :P – I Hate Lazy Commented Oct 1, 2012 at 0:10
- @user1689607 I don't know, the question just felt inplete without it. – Peter Olson Commented Oct 1, 2012 at 0:26
- Well, it was an interesting question anyway. Good to see JS doesn't suffer the 2038 problem. – I Hate Lazy Commented Oct 1, 2012 at 1:01
1 Answer
Reset to default 19According 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 fortably inside of this range.