如何获得最小和最大日期 [英] How to get the minimum and maximum date
问题描述
如何获得最小和最大的日期。我看到最小的数字可以是这样的:
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
推荐答案
日期没有这个
Date does not have this
实际上,确实如此,但只是间接地。根据规范,日期
object的毫秒数 - 因为Epoch值只能在-8640000000000000到8640000000000000的范围内。
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.
所以最小日期是新日期(-8640000000000000)
(星期二,2月20日 - 271821 00:00:00 GMT),最长日期新日期(8640000000000000)
(星期六,9月13日275760 00:00:00 GMT)。
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).
如果你愿意,你可以把它们放在日期
作为属性的函数:
If you wanted, you could put those on the Date
function as properties:
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:
Object.defineProperties(Date, {
MIN_VALUE: {
value: -8640000000000000 // A number, not a date
},
MAX_VALUE: {
value: 8640000000000000
}
});
它定义了日期
中不能存在的属性已更改具有日期的最小/最大数值。 (在支持ES5的JavaScript引擎上。)
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.)
这篇关于如何获得最小和最大日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!