Skip to content
Advertisement

Moment.js compare two date thrown a warning

I have created a simple app, which need a date comparison. I used Moment.js, and I have tried on answer on this question:

Compare two dates in JS

Moment js date time comparison

How to compare only date in moment.js

But all of them not working for me.

and now I use this code:

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

But in the console it’s thrown a warning:

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.

Everybody please help me. here’s my fiddle https://jsfiddle.net/gq6ykw8L/

Advertisement

Answer

Your date string is ambiguous between DD/MM/YYYY and MM/DD/YYYY. If you refer to the link given in the warning (http://momentjs.com/guides/#/warnings/js-date/), it says:

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().

You need to use moment(String, Format) to specify the format of your date string.

moment('09/12/2016', 'DD/MM/YYYY');
moment('09/12/2016', 'MM/DD/YYYY');
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement