4

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/

Community
  • 1
  • 1
Engkus Kusnadi
  • 2,386
  • 2
  • 18
  • 40

1 Answers1

3

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');
Ozan
  • 3,709
  • 2
  • 18
  • 23
  • @EngkusKusnadi, The date string inside isAfter is throwing the warning in the fiddle. Declare the second date as moment using moment(String, Format) too. See https://jsfiddle.net/ozzan/gq6ykw8L/4/ – Ozan Aug 09 '16 at 05:23
  • They could have added the examples that you included in your answer. But they chose not to. Thanks for this answer. – Lucio Mollinedo Mar 31 '22 at 23:01