如何仅比较moment.js中的日期 [英] How to compare only date in moment.js
问题描述
我是moment.js的新手。我有一个日期对象,它有一些时间与它相关联。我只想检查该日期是否大于或等于今天的日期, 不包括比较的时间。
I am new to moment.js. I have a date object and it has some time associated with it. I just want to check if that date is greater than or equal to today's date, excluding the time when comparing.
var dateToCompare = 2015-04-06T18:30:00.000Z
我只想检查dateToCompare是否等于或大于今天的日期。
我已经检查了is.ame of moment.js,但它似乎是字符串而且只是日期部分。但我不想将我的日期转换为字符串或进一步操纵它。因为我担心javascript可能会在将该日期转换为字符串时出现意外情况(例如添加偏移量或dst等),或者可能是我错了。
I just want to check if dateToCompare is equal or greater than today's date. I have checked isSame of moment.js, but it seems to take string and only the date part. But I do not want to convert my date to string or manipulate it further. Because I am worried that javascript may do something unexpected when converting that date to string(like adding the offset or dst,etc), or may be I am wrong.
示例isSame()from docs
Sample isSame() from docs
moment('2010-10-20').isSame('2010-10-20');
此外,我正在寻找像isSame()和isAfter()这样的东西作为一个陈述。
Also I am looking for something like isSame() and isAfter() combined as one statement.
我需要仅使用moment.js进行比较。请不要建议普通的javascript日期比较。
I need to compare using moment.js only.Please do not suggest plain javascript date comparison.
推荐答案
文档非常清楚您传递了第二个参数来指定粒度。
The docs are pretty clear that you pass in a second parameter to specify granularity.
如果要将粒度限制为毫秒以外的单位,请将单位作为第二个参数传递。
If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.
moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
moment('2010-10-20').isAfter('2009-12-31', 'year'); // true
第二个参数确定精度,而不仅仅是要检查的单个值,使用一天将检查年,月和日。
As the second parameter determines the precision, and not just a single value to check, using day will check for year, month and day.
对于您的情况,您将通过'day'
作为第二个参数。
For your case you would pass 'day'
as the second parameter.
这篇关于如何仅比较moment.js中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!