如何获得最小和最大日期

时间:2021-08-17 16:01:32

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

日期没有这个。有没有办法找到最小和最大的日期

1 个解决方案

#1


21  

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.

事实上,它是这样的,但只是间接的。根据规范,一个日期对象的毫秒值只能在-864000000000到864000000000之间。

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).

所以最小的日期是新的日期(-8640000000000000)(Tue, 4月20日-271821 00:00 00:00 GMT),最大的日期是新的日期(8640000000000000)(Sat, 9月13日275760 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:

…但是由于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.)

它定义了日期上不能更改的属性,这些属性的日期值为最小/最大值。(在支持ES5的JavaScript引擎上)

#1


21  

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.

事实上,它是这样的,但只是间接的。根据规范,一个日期对象的毫秒值只能在-864000000000到864000000000之间。

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).

所以最小的日期是新的日期(-8640000000000000)(Tue, 4月20日-271821 00:00 00:00 GMT),最大的日期是新的日期(8640000000000000)(Sat, 9月13日275760 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:

…但是由于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.)

它定义了日期上不能更改的属性,这些属性的日期值为最小/最大值。(在支持ES5的JavaScript引擎上)