Javascript 如何只比较moment.js中的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29494528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:27:41  来源:igfitidea点击:

How to compare only date in moment.js

javascriptmomentjs

提问by NoobGeek

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.

我是 moment.js 的新手。我有一个日期对象,它有一些与之相关的时间。我只想检查该日期是否大于或等于今天的日期,不包括比较时的时间。

 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 是否等于或大于今天的日期。我已经检查过 moment.js 的 isSame,但它似乎只需要字符串和日期部分。但我不想将我的日期转换为字符串或进一步操作它。因为我担心 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.

我只需要使用 moment.js 进行比较。请不要建议使用纯 javascript 日期比较。

回答by zzzzBov

The docs are pretty clearthat 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.

如果要将粒度限制为毫秒以外的单位,请将单位作为第二个参数传递。

moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
moment('2010-10-20').isAfter('2009-12-31', 'year'); // true

由于第二个参数决定了精度,而不仅仅是要检查的单个值,因此使用 day 将检查年、月和日。

For your case you would pass 'day'as the second parameter.

对于您的情况,您将'day'作为第二个参数传递。

回答by sandrooco

Meanwhile you can use the isSameOrAftermethod:

同时您可以使用以下isSameOrAfter方法:

moment('2010-10-20').isSameOrAfter('2010-10-20', 'day');

回答by Renish Gotecha

In my case i did following code for compare 2 dates may it will help you ...

在我的情况下,我做了以下代码来比较 2 个日期可能会帮助你......

var date1 = "2010-10-20";
var date2 = "2010-10-20";
var time1 = moment(date1).format('YYYY-MM-DD');
var time2 = moment(date2).format('YYYY-MM-DD');
if(time2 > time1){
 console.log('date2 is Greter than date1');
}else if(time2 > time1){
 console.log('date2 is Less than date1');
}else{
 console.log('Both date are same');
}
<script src="https://momentjs.com/downloads/moment.js"></script>

回答by Anurag Aron Tigga

You could use startOf('day')method to compare just the date

您可以使用startOf('day')方法来比较日期

Example :

例子 :

var dateToCompare = moment("06/04/2015 18:30:00");
var today = moment(new Date());

dateToCompare.startOf('day').isSame(today.startOf('day'));