您還可以檢查此reference這給例子,什麼是在Javascript的時間範圍。
以下是參考代碼: -
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);}
有趣的是,日期'新的日期(99999,11,31,9999999)'是有效的。它給了我Wed 10 16 101140 15:00:00 GMT-0400(EDT)。 –