9

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?

3
  • 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 Commented Oct 1, 2012 at 0:10
  • 1
    @user1689607 I don't know, the question just felt incomplete without it. Commented Oct 1, 2012 at 0:26
  • Well, it was an interesting question anyway. Good to see JS doesn't suffer the 2038 problem. Commented Oct 1, 2012 at 1:01

1 Answer 1

19

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I believe the date for the past has a typo. This is what I get new Date(-8640000000000000).toUTCString() = 'Tue, 20 Apr -271821 00:00:00 GMT'
@Rohit Notice the 1 at the end. new Date(-8640000000000001) is Invalid Date, while new Date(-8640000000000000) is the date you got, the earliest valid date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.