Date()
对象中的 JavaScript 年份范围是多少
我知道在 MS SQL
中,datetime
数据类型的年份日期范围是从 1753-01-01 到 9999-12-31
即使在 JavaScript 月份范围是 0-11 中,任何其他日期都将是无效日期
我正在对此进行测试,以下是一些结果:
new Date(99999,1,1) ; //is valid
但是
new Date(1000000,1,1); // invalid
这意味着有一个正确的年份范围 任何人都知道它是什么以及为什么这个确切的范围?
最佳答案
来自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
所以如果我说:
var d = new Date();
d.setTime(8640000000000000);
document.write(d);
然后它会输出为:
9 月 12 日星期五 275760 20:00:00 GMT-0400(东部夏令时间)
但是d.setTime(8640000000000001);
不会工作
关于javascript - JavaScript 日期范围是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20151976/