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
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.
我只是想检查dateToCompare是否等于或大于今天的日期。我也查过了。但它似乎只带了字符串和日期部分。但我不想将日期转换为字符串或进一步操作它。因为我担心javascript在将日期转换为字符串时可能会做一些意想不到的事情(比如添加偏移量或dst等),或者我可能是错的。
Sample isSame() from docs
从文档样本isSame()
moment('2010-10-20').isSame('2010-10-20');
Also I am looking for something like isSame() and isAfter() combined as one statement.
我还在寻找一些像isSame()和isAfter()这样的组合,作为一个语句。
I need to compare using moment.js only.Please do not suggest plain javascript date comparison.
我需要比较使用力矩。js。请不要建议简单的javascript日期比较。
2 个解决方案
#1
108
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.
对于您的情况,您将通过“day”作为第二个参数。
#2
15
Meanwhile you can use the isSameOrAfter
method:
同时可以使用isSameOrAfter方法:
moment('2010-10-20').isSameOrAfter('2010-10-20', 'day');
#1
108
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.
对于您的情况,您将通过“day”作为第二个参数。
#2
15
Meanwhile you can use the isSameOrAfter
method:
同时可以使用isSameOrAfter方法:
moment('2010-10-20').isSameOrAfter('2010-10-20', 'day');