如何仅比较时刻的日期。
我是新手。我有一个日期对象,它有一段时间与之相关。我只想检查该日期是否大于或等于今天的日期, 不包括比较的时间。
var dateToCompare = 2015-04-06T18:30:00.000Z
我只想检查dateToCompare是等于还是大于今天的 时间日期。 我已经检查了Moment.js的Issame,但似乎需要字符串,仅需日期部分。但是我不想将日期转换为字符串或进一步操纵。因为我担心在将日期转换为字符串(例如添加偏移或dst等)时,JavaScript可能会做一些意外的事情,或者我错了。
sample insame()来自文档
moment('2010-10-20').isSame('2010-10-20');
也是我正在寻找Issame()和iSafter()之类的东西,合并为一个语句。
我需要比较使用Moment.js。请不要建议简单的JavaScript日期比较。
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.
Sample isSame() from docs
moment('2010-10-20').isSame('2010-10-20');
Also I am looking for something like isSame() and isAfter() combined as one statement.
I need to compare using moment.js only.Please do not suggest plain javascript date comparison.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
文档很清楚您通过第二个参数来指定粒度。
对于您的情况,您将通过
'Day'
作为第二个参数。The docs are pretty clear that you pass in a second parameter to specify granularity.
For your case you would pass
'day'
as the second parameter.同时,您可以使用
issameOrafter
方法:docs: https://momentjs.com/docs/#/query/is-same-same-or-after/
Meanwhile you can use the
isSameOrAfter
method:Docs: https://momentjs.com/docs/#/query/is-same-or-after/
在我的情况下,我确实按照代码进行比较2日期可能会帮助您...
In my case i did following code for compare 2 dates may it will help you ...
continue
You could use
startOf('day')
method to compare just the dateExample :
检查
2022-01-07与当前日期有关,
如果当前日期已超过2022年1月6日,则返回false!
checking
2022-01-07 this with current date
if current date is beyond 6th Jan 2022, it returns false!
检查一个日期是通过使用
iSafter()
方法来的。通过使用
iSbefore()
方法,检查一个日期是另一个日期。通过使用
issame()
方法,检查一个日期与另一个日期相同。For checking one date is after another by using
isAfter()
method.For checking one date is before another by using
isBefore()
method.For checking one date is same as another by using
isSame()
method.力矩('date')。todate()。getTime()>片刻()
moment('date').toDate().getTime() > moment()