Javascript 最小和最大日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11526504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Minimum and maximum date
提问by MaxArt
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.
我想知道 JavascriptDate
对象允许的最小和最大日期是什么。我发现最小日期大约是公元前 200000 年,但我找不到任何关于它的参考。
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.
“纪元时间”(= 1970-01-01 00:00:00 UTC+00 的毫秒数)的答案将是最好的。
回答by T.J. Crowder
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.
一个 Date 对象包含一个 Number ,指示一个特定的时间到一毫秒内。这样的数字称为时间值。时间值也可能是 NaN,表示 Date 对象不代表特定的时刻。
时间在 ECMAScript 中以 UTC 时间 1970 年 1 月 1 日以来的毫秒为单位进行测量。在时间值中,闰秒被忽略。假设每天正好有 86,400,000 毫秒。ECMAScript Number 值可以表示从 –9,007,199,254,740,992 到 9,007,199,254,740,992 的所有整数;此范围足以测量从 UTC 时间 1970 年 1 月 1 日起向前或向后大约 285,616 年内的任何时刻的毫秒精度。
ECMAScript Date 对象支持的实际时间范围略小:相对于 UTC 时间 1970 年 1 月 1 日开始的午夜,正好是 –100,000,000 天到 100,000,000 天。这为 1970 年 1 月 1 日 UTC 的任一侧提供了 8,640,000,000,000,000 毫秒的范围。
UTC 时间 1970 年 1 月 1 日开始的午夜的确切时刻由值 +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).
第三段是最相关的。根据该段落,我们可以从 中获得每个规范的准确最早日期new Date(-8640000000000000)
,即公元前 271,821 年 4 月 20 日,星期二(公元前 =共纪之前,例如 -271,821 年)。
回答by Shaun Luttin
To augment T.J.'s answer, exceeding the min/max values generates an Invalid Date.
为了增加 TJ 的答案,超过最小/最大值会生成无效日期。
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
回答by ColacX
As you can see, 01/01/1970 returns 0, which means it is the lowest possible date.
如您所见,01/01/1970 返回 0,这意味着它是可能的最低日期。
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