对于momentjs,我如何知道两个时刻是否代表同一天(不一定是同一时间)?

时间:2022-09-13 21:28:21

I have 2 momentjs objects, moment1 and moment2:

我有两个动量物体,动量1和动量2:

对于momentjs,我如何知道两个时刻是否代表同一天(不一定是同一时间)?

Why is moment1.isSame(moment2, 'date') returning false??

为什么moment1。isSame(moment2“日期”)返回false ? ?

My understanding is that checking moment1.isSame(moment2, 'day') returns whether they are the same day of the week (at least, that's what it looks like from the docs). So if both 'day' and 'date' don't work, what is the correct way to determine if the 2 dates represent the same day?

我的理解是检查动量。isSame(moment2,“day”)返回是否为一周的同一天(至少,文档上是这样)。如果" day "和" date "都不行,用什么方法来确定这两个日期是否代表同一天?

I could have sworn I've used moment1.isSame(moment2, 'date') in the past, but I must be remembering incorrectly...

我可以发誓我用了动量。在过去是一样的,但我一定是记错了……

1 个解决方案

#1


6  

You can use both 'day' and 'date' to isSame.

你可以同时使用“day”和“date”。

As the docs says:

医生说:

Check if a moment is the same as another moment.

检查一个时刻是否与另一个时刻相同。

When including a second parameter, it will match all units equal or larger. Passing in month will check month and year. Passing in day will check day, month, and year.

当包含第二个参数时,它将匹配所有相等或大于的单元。月复一月,年复一年。经过一天会检查一天,一个月,一年。

Like moment#isAfter and moment#isBefore, any of the units of time that are supported for moment#startOf are supported for moment#isSame.

就像时刻#isAfter和时刻#isBefore一样,时刻#startOf支持的任何时间单位都支持时刻#isSame。

In the docs of startOf:

在startOf的文档中:

Note: moment#startOf('date') was added as an alias for day in 2.13.0

注意:在2.13.0中,时刻#startOf(“date”)被添加为一天的别名

Here a working example with the lastest version (2.17.1):

这里有一个最新版本的工作示例(2.17.1):

var moment1 = moment('01/23/17', 'MM/D/YYYY');
var moment2 = moment('01/23/17', 'MM/D/YYYY');
console.log( moment1.isSame(moment2, 'day') ); // true
console.log( moment1.isSame(moment2, 'date') ); // true
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

#1


6  

You can use both 'day' and 'date' to isSame.

你可以同时使用“day”和“date”。

As the docs says:

医生说:

Check if a moment is the same as another moment.

检查一个时刻是否与另一个时刻相同。

When including a second parameter, it will match all units equal or larger. Passing in month will check month and year. Passing in day will check day, month, and year.

当包含第二个参数时,它将匹配所有相等或大于的单元。月复一月,年复一年。经过一天会检查一天,一个月,一年。

Like moment#isAfter and moment#isBefore, any of the units of time that are supported for moment#startOf are supported for moment#isSame.

就像时刻#isAfter和时刻#isBefore一样,时刻#startOf支持的任何时间单位都支持时刻#isSame。

In the docs of startOf:

在startOf的文档中:

Note: moment#startOf('date') was added as an alias for day in 2.13.0

注意:在2.13.0中,时刻#startOf(“date”)被添加为一天的别名

Here a working example with the lastest version (2.17.1):

这里有一个最新版本的工作示例(2.17.1):

var moment1 = moment('01/23/17', 'MM/D/YYYY');
var moment2 = moment('01/23/17', 'MM/D/YYYY');
console.log( moment1.isSame(moment2, 'day') ); // true
console.log( moment1.isSame(moment2, 'date') ); // true
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>