关于javascript:Moment.js比较两个日期引发警告

Moment.js compare two date thrown a warning

我创建了一个简单的应用程序,需要进行日期比较。 我使用了Moment.js,并尝试回答以下问题:

比较JS中的两个日期

Moment JS日期时间比较

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

但是他们都不为我工作。

现在我使用此代码:

1
2
3
4
5
if(moment('09/12/2016').isAfter('09/11/2016')){
    console.log("True")  
} else {
    console.log("False")
}

但是在控制台中,它引发了警告:

Deprecation warning: moment construction falls back to js Date. This
is discouraged and will be removed in upcoming major release. Please
refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

大家请帮帮我。 这是我的小提琴https://jsfiddle.net/gq6ykw8L/


您的日期字符串在DD/MM/YYYYMM/DD/YYYY之间不明确。 如果您参考警告中提供的链接(http://momentjs.com/guides/#/warnings/js-date/),则它表示:

This deprecation warning is thrown when no known format is found for a
date passed into the string constructor. To work around this issue,
specify a format for the string being passed to moment().

您需要使用moment(String, Format)指定日期字符串的格式。

1
2
moment('09/12/2016', 'DD/MM/YYYY');
moment('09/12/2016', 'MM/DD/YYYY');

  • @ EngkusKusnadi,isAfter中的日期字符串在警告中发出警告。 也使用moment(String,Format)将第二个日期声明为一刻。 参见jsfiddle.net/ozzan/gq6ykw8L/4