Skip to content Skip to sidebar Skip to footer

Moment.js Deprecation Warning When Comparing Two Dates

I didn't see any SO thread that matches my use case. I'm trying to compare two dates to see if they match or not. If they don't, then I display an error message. The date format fo

Solution 1:

Neither 'MM/DD/YYYY' nor 'M/D/YYYY' are ISO 8601 compliant format recognized by moment.ISO_8601 See moment(String) docs to see what are ISO 8601 formats recognized by momentjs. You have to specify 'MM/DD/YYYY' (or 'M/D/YYYY') format instead of using moment.ISO_8601.

You can use both 'MM/DD/YYYY' and 'M/D/YYYY' using moment(String, String[]). This is required if you are using strict parsing. Please note that:

Starting in version 2.3.0, Moment uses some simple heuristics to determine which format to use. In order:

  • Prefer formats resulting in valid dates over invalid ones.
  • Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
  • Prefer formats earlier in the array than later.

For example, using ['MM/DD/YYYY', 'MM/DD/YYYY'], ambigous inputs like 01/10/2017 will always be interpreted as 10th of January.

Here an example that do not has Deprecation warning:

var start_date = '11/25/2017';
var user_start_date = '27/12/2017';

if( !moment( start_date, ['MM/DD/YYYY', 'M/D/YYYY']  ).isSame( moment(user_start_date, ['MM/DD/YYYY', 'M/D/YYYY'] )) )
{
    console.log("Both dates must match!");
}
else{
    console.log("Dates match!");
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

Solution 2:

I don't actually see that issue.

http://jsfiddle.net/dbkidd/n9s082bj/

Can you reproduce it in the Fiddle?

var start_date = moment().format('MMM DD h:mm A');
var user_start_date = moment().format('MMM DD h:mm A');

if( !moment( start_date, moment.ISO_8601 ).isSame( user_start_date, 
  moment.ISO_8601 ) )
 {
   console.log("Both dates must match!");
 }
 else{
   console.log("Dates match!");
 }

Post a Comment for "Moment.js Deprecation Warning When Comparing Two Dates"