how to handle deprecation warning in momentjs

Below works for me to remove RFC2822 warnings

Use moment(String, FormatString) to convert string to date.

 var newDt = Moment(this.state.dob,"MM/DD/YY")

And below code from date to String

var dt = Moment(newDt).format("YYYY-MM-DD") 

By this way it will not show warning messages.


Specify the string format for your date then the warning will go away

moment("2017-1-27", 'YYYY-M-D').format('DD MMMM YYYY')

You have to use moment(String, String); to parse your input. If you don't want to specify a format (or an array of formats), you can use moment.ISO_8601. As the docs says:

Moment already supports parsing iso-8601 strings, but this can be specified explicitly in the format/list of formats when constructing a moment

This way you will not have deprecation warning. Here a working example:

var invalid = '2017-03-18 23;00;00';
if (moment(invalid, moment.ISO_8601).isValid()) {
  console.log('valid date');
} else {
  console.log('invalid date');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

PS. Anyway, if you have a list of accepted format, I suggest to use moment(String, String[]); (and strict parsing, if needed).

Tags:

Momentjs