Deprecation Warning in Moment.Js - Not in a Recognized Iso Format

Deprecation warning in Moment.js - Not in a recognized ISO format

Check out all their awesome documentation!

Here is where they discuss the Warning Message.

String + Format

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

moment("12-25-1995", "MM-DD-YYYY");

String + Formats (multiple formats)

If you have more than one format, check out their String + Formats (with an 's').

If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.

moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);

Please check out the documentation for anything more specific.

Timezone

Check out Parsing in Zone, the equivalent documentation for timezones.

The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier.

var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");

EDIT

//...
var dateFormat = "YYYY-M-D H:m"; //<-------- This part will get rid of the warning.
var aus1_s, aus2_s, aus3_s, aus4_s, aus5_s, aus6_s, aus6_e;
if ($("#custom1 :selected").val() == "AU" ) {
var region = 'Australia/Sydney';

aus1_s = moment.tz('2016-9-26 19:30', dateFormat, region);
aus2_s = moment.tz('2016-10-2 19:30', dateFormat, region);
aus3_s = moment.tz('2016-10-9 19:30', dateFormat, region);
aus4_s = moment.tz('2016-10-16 19:30', dateFormat, region);
aus5_s = moment.tz('2016-10-23 19:30', dateFormat, region);
aus6_s = moment.tz('2016-10-30 19:30', dateFormat, region);
aus6_e = moment.tz('2016-11-5 19:30', dateFormat, region);
} else if ($("#custom1 :selected").val() == "NZ" ) {
var region = 'Pacific/Auckland';

aus1_s = moment.tz('2016-9-28 20:30', dateFormat, region);
aus2_s = moment.tz('2016-10-4 20:30', dateFormat, region);
aus3_s = moment.tz('2016-10-11 20:30', dateFormat, region);
aus4_s = moment.tz('2016-10-18 20:30', dateFormat, region);
aus5_s = moment.tz('2016-10-25 20:30', dateFormat, region);
aus6_s = moment.tz('2016-11-2 20:30', dateFormat, region);
aus6_e = moment.tz('2016-11-9 20:30', dateFormat, region);
}
//...

ISO format deprecation warning when providing moment in string format

As explained in the comments above, do your date comparison using moment instances.

The value returned by .format() is a string and depending on the format chosen (and possibly your locale) may trigger the warning you're seeing.

Use .format() when you want to display a value.

const DATE_FORMAT = "YYYY-MM-DD";const endDate = "2020-05-05T00:00:00.000Z" //(dynamic value from service)const appValidDate = moment(endDate).subtract(1, "days");const currentDate = moment().startOf("day");// or for a UTC "start of day"// const currentDate = moment.utc().startOf('day')const validDate = currentDate.isSameOrBefore(appValidDate);
console.log('appValidDate:', appValidDate.format(DATE_FORMAT))console.log('currentDate:', currentDate.format(DATE_FORMAT))console.log('validDate:', validDate)
<script src="https://momentjs.com/downloads/moment.js"></script>

Value provided is not in a recognized RFC2822 or ISO format - Vue js

Both of your approaches, as you warning tells you, do mismatch RFC2822/ISO8601.

RFC2822 would look like this:

01 Jun 2020 14:11:32 -0700

ISO8601 would look like this:

2021-10-10T22:00:00.000Z

If you need to get the date format of vue-moment into ISO8601 format, you can do this in a short function like this:

showNewDate(date) {
return new Date(Date.parse(date))
}

If you pass 2021-10-11 11:17:03 to this function it will give you 2021-10-11T09:17:03.000Z which is the date +- your timezone, in my example case minus two hours.

how to handle deprecation warning in momentjs

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>

momentjs deprecation warning

Try this :

let thisDate = moment(new Date().toISOString()).format("YYYY-MM-DD")

or better

let thisDate = moment().format('YYYY-MM-DD')

Explication :

new Date()

Thu Oct 19 2017 17:50:06 GMT+0200 (Paris, Madrid (heure d’été))
==> depending of local browser and browser implementation

new Date().toISOString()

"2017-10-19T15:50:31.690Z"
==> fixed format, it's always safe ! ;-)

“Deprecation warning: moment construction falls back to js Date” when trying to convert RFC2822 date in moment.js

To get rid of the warning, you need to either:

  • Pass in an ISO formatted version of your date string:

    moment('2014-04-23T09:54:51');

  • Pass in the string you have now, but tell Moment what format the string is in:

    moment('Wed, 23 Apr 2014 09:54:51 +0000', 'ddd, DD MMM YYYY HH:mm:ss ZZ');

  • Convert your string to a JavaScript Date object and then pass that into Moment:

    moment(new Date('Wed, 23 Apr 2014 09:54:51 +0000'));

The last option is a built-in fallback that Moment supports for now, with the deprecated console warning. They say they won't support this fallback in future releases. They explain that using new Date('my date') is too unpredictable.



Related Topics



Leave a reply



Submit