What is the JavaScript year Range in Date()
object
i know in MS SQL
year date range is for datetime
data type is from 1753-01-01 through 9999-12-31
and even in JavaScript month range is 0- 11 any thing else will be invalid date
i was testing this and here is some of the results :
new Date(99999,1,1) ; //is valid
But
new Date(1000000,1,1); // invalid
that means there is a year range right any body knows what is it and why this exact range ?
What is the JavaScript year Range in Date()
object
i know in MS SQL
year date range is for datetime
data type is from 1753-01-01 through 9999-12-31
and even in JavaScript month range is 0- 11 any thing else will be invalid date
i was testing this and here is some of the results :
new Date(99999,1,1) ; //is valid
But
new Date(1000000,1,1); // invalid
that means there is a year range right any body knows what is it and why this exact range ?
Share Improve this question edited Nov 22, 2013 at 19:11 Rahul Tripathi 173k33 gold badges291 silver badges339 bronze badges asked Nov 22, 2013 at 18:30 Mina GabrielMina Gabriel 25.2k26 gold badges100 silver badges128 bronze badges 1-
1
Interestingly enough, the date
new Date(99999,11,31,9999999)
is valid. It gives me Wed Oct 16 101140 15:00:00 GMT-0400 (EDT). – Joshua Dwire Commented Nov 22, 2013 at 18:35
2 Answers
Reset to default 12From the docs:
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
So if I say like:
var d = new Date();
d.setTime(8640000000000000);
document.write(d);
Then it would output as:
Fri Sep 12 275760 20:00:00 GMT-0400 (Eastern Daylight Time)
But d.setTime(8640000000000001);
will not work
You may also check this reference which gives the example as to what is the date range in Javascript.
Below is the code for reference:-
try{
module("ES5 ranges");
test("ES5 Epoch", function() {
// epoch
var expected = 0;
var actual = new Date(expected);
same(actual.getTime(), expected, "");
same(actual.getUTCFullYear(), 1970, "");
same(actual.getUTCMonth(), 0, "");
same(actual.getUTCDate(), 1, "");
same(actual.getUTCHours(), 0, "");
same(actual.getUTCMinutes(), 0, "");
same(actual.getUTCSeconds(), 0, "");
same(actual.getUTCMilliseconds(), 0, "");
});
test("ES5 Min Date", function() {
// epoch - 1e8 days
var expected = -8.64e15;
var actual = new Date(expected);
same(actual.getTime(), expected, "");
same(actual.getUTCFullYear(), -271821, "");
same(actual.getUTCMonth(), 3, "");
same(actual.getUTCDate(), 20, "");
same(actual.getUTCHours(), 0, "");
same(actual.getUTCMinutes(), 0, "");
same(actual.getUTCSeconds(), 0, "");
same(actual.getUTCMilliseconds(), 0, "");
});
test("ES5 Max Date", function() {
// epoch + 1e8 days
var expected = 8.64e15;
var actual = new Date(expected);
same(actual.getTime(), expected, "");
same(actual.getUTCFullYear(), 275760, "");
same(actual.getUTCMonth(), 8, "");
same(actual.getUTCDate(), 13, "");
same(actual.getUTCHours(), 0, "");
same(actual.getUTCMinutes(), 0, "");
same(actual.getUTCSeconds(), 0, "");
same(actual.getUTCMilliseconds(), 0, "");
});
module("Overflow Date ranges");
test("Underflow Date", function() {
// epoch - 1e8 days - 1 ms
var expected = -8.64e15-1;
var actual = new Date(expected);
same(actual.getTime(), NaN, "");
same(actual.getUTCFullYear(), NaN, "");
same(actual.getUTCMonth(), NaN, "");
same(actual.getUTCDate(), NaN, "");
same(actual.getUTCHours(), NaN, "");
same(actual.getUTCMinutes(), NaN, "");
same(actual.getUTCSeconds(), NaN, "");
same(actual.getUTCMilliseconds(), NaN, "");
});
test("Overflow Date", function() {
// epoch + 1e8 days + 1 ms
var expected = 8.64e15+1;
var actual = new Date(expected);
same(actual.getTime(), NaN, "");
same(actual.getUTCFullYear(), NaN, "");
same(actual.getUTCMonth(), NaN, "");
same(actual.getUTCDate(), NaN, "");
same(actual.getUTCHours(), NaN, "");
same(actual.getUTCMinutes(), NaN, "");
same(actual.getUTCSeconds(), NaN, "");
same(actual.getUTCMilliseconds(), NaN, "");
});
}catch(ex){alert(ex);}