javascript 如何获得最小和最大日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27093130/
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
How to get the minimum and maximum date
提问by Luke101
How do I get the smallest and biggest date. I see that the smallest number can be got like this:
我如何获得最小和最大的日期。我看到最小的数字可以这样得到:
Number.MIN_VALUE
Date does not have this. Is there a way to find the smallest and biggest date
日期没有这个。有没有办法找到最小和最大的日期
回答by T.J. Crowder
Date does not have this
日期没有这个
Actually, it does, but only indirectly. According to the specification, a Date
object's milliseconds-since-the-Epoch value can only be in the range -8640000000000000 to 8640000000000000.
事实上,确实如此,但只是间接的。根据规范,Date
对象的自纪元以来的毫秒值只能在 -8640000000000000 到 8640000000000000 的范围内。
So the minimum date is new Date(-8640000000000000)
(Tue, 20 Apr -271821 00:00:00 GMT), and the maximum date is new Date(8640000000000000)
(Sat, 13 Sep 275760 00:00:00 GMT).
所以最小日期是new Date(-8640000000000000)
(Tue, 20 Apr -271821 00:00:00 GMT),最大日期是new Date(8640000000000000)
(Sat, 13 Sep 275760 00:00:00 GMT)。
If you wanted, you could put those on the Date
function as properties:
如果你愿意,你可以把它们Date
作为属性放在函数上:
Date.MIN_VALUE = new Date(-8640000000000000);
Date.MAX_VALUE = new Date(8640000000000000);
...but since Date
instances are mutable, I probably wouldn't, because it's too easy to accidentally modify one of them. An alternative would be to do this:
...但由于Date
实例是可变的,我可能不会,因为意外修改其中之一太容易了。另一种方法是这样做:
Object.defineProperties(Date, {
MIN_VALUE: {
value: -8640000000000000 // A number, not a date
},
MAX_VALUE: {
value: 8640000000000000
}
});
That defines properties on Date
that cannot be changed that have the min/max numeric value for dates. (On a JavaScript engine that has ES5 support.)
这定义了Date
不能更改的属性,这些属性具有日期的最小/最大数值。(在支持 ES5 的 JavaScript 引擎上。)