181

I was wondering which is the minimum and the maximum date allowed for a Javascript Date object. I found that the minimum date is something like 200000 B.C., but I couldn't get any reference about it.

Does anyone know the answer? I just hope that it doesn't depend on the browser.

An answer in "epoch time" (= milliseconds from 1970-01-01 00:00:00 UTC+00) would be the best.

MaxArt
  • 22,200
  • 10
  • 82
  • 81
  • 1
    For a copy/paste answer: http://stackoverflow.com/questions/27093130/how-to-get-the-minimum-and-maximum-date – Kieveli Nov 08 '16 at 20:21

5 Answers5

220

From the spec, §15.9.1.1:

A Date object contains a Number indicating a particular instant in time to within a millisecond. Such a Number is called a time value. A time value may also be NaN, indicating that the Date object does not represent a specific instant of time.

Time is measured in ECMAScript in milliseconds since 01 January, 1970 UTC. In time values leap seconds are ignored. It is assumed that there are exactly 86,400,000 milliseconds per day. ECMAScript Number values can represent all integers from –9,007,199,254,740,992 to 9,007,199,254,740,992; this range suffices to measure times to millisecond precision for any instant that is within approximately 285,616 years, either forward or backward, from 01 January, 1970 UTC.

The actual range of times supported by ECMAScript Date objects is slightly smaller: 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.

The exact moment of midnight at the beginning of 01 January, 1970 UTC is represented by the value +0.

The third paragraph being the most relevant. Based on that paragraph, we can get the precise earliest date per spec from new Date(-8640000000000000), which is Tuesday, April 20th, 271,821 BCE (BCE = Before Common Era, e.g., the year -271,821).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    So that's why it doesn't work with -9223372036854775808L... Even after i change it to date the resever return positive out of range value. – Hassan Faghihi Nov 28 '16 at 08:47
  • 1
    I don't know what people are complaining about, this works fine for me. BTW I wonder if there's any way this works in reverse (i.e. entering the BCE date and getting the epoch seconds popped out as a negative int)? – Mark Seagoe Feb 05 '19 at 19:12
  • 1
    @MarkSeagoe - `Date.UTC(-271821, 3, 20)` returns `-8640000000000000`. :-) – T.J. Crowder Feb 06 '19 at 07:29
  • new Date('275760-9-13 05:30:00') – AshTyson Nov 09 '19 at 12:00
  • 1
    @AshTyson - If you're asking why that gives you `Invalid Date`, there are three reasons: 1. If you have more than four year digits, you must use a `+` or `-` at the beginning. 2. It's missing the `0` on `09`. 3. It's only in range in some timezones (specifically: GMT+05:30 or more), since the latest date is 275760-09-13 at midnight GMT. More on the format [here](https://tc39.es/ecma262/#sec-date-time-string-format). So in GMT+05:30 (or +06:00 etc.), this would work: `new Date("+275760-09-13T05:30:00.000")` – T.J. Crowder Nov 10 '19 at 09:43
  • Im not asking I mentioned that it is the maximum date range of the date object. Im not able to edit it. – AshTyson Nov 10 '19 at 20:42
  • @AshTyson - Right. That's what the answer says. What's the point of the comment? – T.J. Crowder Nov 11 '19 at 07:51
  • The answer is way off. IT can support longer dates. Please compare the dates given – AshTyson Nov 13 '19 at 03:18
  • @AshTyson - What specifically about the specification quote is unclear? The [wording](https://tc39.es/ecma262/#sec-time-values-and-time-range) is a bit different now, but it's still the same range. I suppose an implementation is allowed to exceed the range, but I wouldn't count on it, and V8 (in Chrome) doesn't. *"Please compare the dates given"* What dates given? – T.J. Crowder Nov 13 '19 at 07:03
  • If anyone wants a more concise way to write this: `const maxDate = new Date(864e13);` – Kelly Selden Jul 28 '21 at 17:38
91

To augment T.J.'s answer, exceeding the min/max values generates an Invalid Date.

let maxDate = new Date(8640000000000000);
let minDate = new Date(-8640000000000000);

console.log(new Date(maxDate.getTime()).toString());
console.log(new Date(maxDate.getTime() - 1).toString());
console.log(new Date(maxDate.getTime() + 1).toString()); // Invalid Date

console.log(new Date(minDate.getTime()).toString());
console.log(new Date(minDate.getTime() + 1).toString());
console.log(new Date(minDate.getTime() - 1).toString()); // Invalid Date
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
11

A small correction of the accepted answer; the year of the minimum date is actually 271,822 BCE, as you can see when running the following snippet:

console.log(new Date(-8640000000000000).toLocaleString("en", {"year": "numeric", "era": "short"}))

Indeed, year -271,821 is actually 271,822 BCE because JavaScript's Date (along with ISO 8601) uses astronomical year numbering, which uses a year zero. Thus, year 1 is 1 CE, year 0 is 1 BCE, year -1 is 2 BCE, etc.

-2

var startingDate = new Date().toISOString().split('T')[0] + "T00:00:00.001Z"; // for 2022-07-25 00 00 00 var endingDate = new Date().toISOString().split('T')[0] + "T23:59:59.999Z" ;

  • 2
    Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Jul 25 '22 at 18:32
-18

As you can see, 01/01/1970 returns 0, which means it is the lowest possible date.

new Date('1970-01-01Z00:00:00:000') //returns Thu Jan 01 1970 01:00:00 GMT+0100 (Central European Standard Time)
new Date('1970-01-01Z00:00:00:000').getTime() //returns 0
new Date('1970-01-01Z00:00:00:001').getTime() //returns 1
Mason
  • 1,007
  • 1
  • 13
  • 31
ColacX
  • 3,928
  • 6
  • 34
  • 36