我创建了一个简单的应用程序,它需要一个日期比较。我使用了 Moment.js,我已经尝试回答这个问题:
Moment js date time comparison
How to compare only date in moment.js
但所有这些都不适合我。
现在我使用这段代码:
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.
请大家帮帮我。这是我的 fiddle https://jsfiddle.net/gq6ykw8L/
最佳答案
您的日期字符串在 DD/MM/YYYY
和 MM/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)
指定日期字符串的格式。
moment('09/12/2016', 'DD/MM/YYYY');
moment('09/12/2016', 'MM/DD/YYYY');
关于javascript - Moment.js 比较两个日期抛出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842383/