ChatGPT解决这个技术问题 Extra ChatGPT

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

I'm getting a warning that a value provided to moment is not in a recognized ISO format. I changed my variable today with the moment function and still it doesn't work.

Here's the warning error:

Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info. Arguments: [0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object]

var entryDate = new Date();
var currentDate = entryDate.getDate();

function between(x, min, max) {
  return x.valueOf() >= min.valueOf() && x < max.valueOf();
}

$('#custom1').change(function () {
  if ($('#custom1 :selected').val() == 'AU') {
    var keyword = '';

    var aus1_s = moment.tz('2016-9-26 19:30', 'Australia/Sydney');
    var aus2_s = moment.tz('2016-10-2 19:30', 'Australia/Sydney');
    var aus3_s = moment.tz('2016-10-9 19:30', 'Australia/Sydney');
    var aus4_s = moment.tz('2016-10-16 19:30', 'Australia/Sydney');
    var aus5_s = moment.tz('2016-10-23 19:30', 'Australia/Sydney');
    var aus6_s = moment.tz('2016-10-30 19:30', 'Australia/Sydney');
    var aus6_e = moment.tz('2016-11-5 19:30', 'Australia/Sydney');
  } else if ($('#custom1 :selected').val() == 'NZ') {
    var aus1_s = moment.tz('2016-9-28 20:30', 'Pacific/Auckland');
    var aus2_s = moment.tz('2016-10-4 20:30', 'Pacific/Auckland');
    var aus3_s = moment.tz('2016-10-11 20:30', 'Pacific/Auckland');
    var aus4_s = moment.tz('2016-10-18 20:30', 'Pacific/Auckland');
    var aus5_s = moment.tz('2016-10-25 20:30', 'Pacific/Auckland');
    var aus6_s = moment.tz('2016-11-2 20:30', 'Pacific/Auckland');
    var aus6_e = moment.tz('2016-11-9 20:30', 'Pacific/Auckland');
  } else {
    $('#entryEquals').val('');
    return false;
  }

  var today = moment();

  switch (true) {
    case between(today, aus1_s, aus2_s):
      keyword = 'RElYT04=';
      break;

    case between(today, aus2_s, aus3_s):
      keyword = 'QlJJREU=';
      break;

    case between(today, aus3_s, aus4_s):
      keyword = 'U1lETkVZ';
      break;

    case between(today, aus4_s, aus5_s):
      keyword = 'R1JPT00=';
      break;

    case between(today, aus5_s, aus6_s):
      keyword = 'V0VERElORw==';
      break;

    case between(today, aus6_s, aus6_e):
      keyword = 'VExD';
      break;

    default:
      $('#entryEquals').val('');
      break;
  }

  $('#entryEquals').val(keyword);
});
I think the warning refers to '2016-9-26 19:30' and such not being a recognized ISO format
what is the error on the format? so do I need to add 0?
You can do moment.tz('2016-9-28 20:30', 'YYYY-M-D H:mm', 'Pacific/Auckland'); according to the documentation ... this will accept 1 or 2 digit months and days, 1 or 2 digit hours in 24 hour format
The error is that the format you are supplying isn't a recognised ISO format ... either provide a recognised ISO format, or provide a string that "hints" at the expected format
Okay thanks how about this one can i add this format too? var today = moment.tz(todaystring,$.urlParam('t_zone'));

C
Community

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 checkout the documentation for anything more specific.

Timezone

Checkout 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);
}
//...

Rather than copying and pasting documentation, please provide an actual answer.
this is what helped me putting the format to "input" - moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
@Rebs I think I added a lot of documentation because the general solution isn't straight forward. Obviously the specific solution for Jemai should have been in the answer and I have added it since (thanks to you!) but the former is why there's so much documentation here.
Thanks but what if you just want a simple date without this heavy formating framework, we can just ignore this warning maybe ?
this helped me --> moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
f
fedorqui

Doing this works for me:

moment(new Date("27/04/2016")).format

This is the simplest work around to avoid the console pollution
I wouldn't advise this approach for anyone that has to deal with IE support to any degree. Results are very flaky specially on IE 11.
This is the most simplest and accurate answer. Thanks it helped me.
Works great for me. I can also pass the date format at the end: return moment(new Date(value)).format(dateFormat);
Did you say "IE 11", Well, I can't hear you.
M
MD. ABU TALHA

use moment in your function like this

 moment(new Date(date)).format('MM/DD/YYYY')

This is the short answer you are looking for if you just want get rid of the "depreciated warning"
Could you define what date would be in this answer?
S
Shahzeb Khan

I ran into this error because I was trying to pass in a date from localStorage. Passing the date into a new Date object, and then calling .toISOString() did the trick for me:

const dateFromStorage = localStorage.getItem('someDate');
const date = new Date(dateFromStorage);
const momentDate = moment(date.toISOString());

This suppressed any warnings in the console.


This will suppress the warning but downside is if you're specific to timezone, then I would not suggest this : cause it would convert the given date to UTC as we're saying '.toISOString()' . Try "moment(new Date("27/04/2016")).format" as stated from other answer - which would keep the time in local..
buddy you gave the actual solution, thanks
B
BeaST 30

You can use

moment(date,"currentFormat").format("requiredFormat");

This should be used when date is not ISO Format as it'll tell moment what our current format is.


While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@leopal made some changes!
this is correct mode!
S
Saahithyan Vigneswaran

This answer is to give a better understanding of this warning

Deprecation warning is caused when you use moment to create time object, var today = moment();.

If this warning is okay with you then I have a simpler method.

Don't use date object from js use moment instead. For example use moment() to get the current date.

Or convert the js date object to moment date. You can simply do that specifying the format of your js date object.

ie, moment("js date", "js date format");

eg:

moment("2014 04 25", "YYYY MM DD");

(BUT YOU CAN ONLY USE THIS METHOD UNTIL IT'S DEPRECIATED, this may be depreciated from moment in the future)


Seriously! the question about how to remove this deprecation warning! not asking how to use this deprecation! also, it is "this will be removed in the future" not "this may be depreciated from the moment in the future" it is already deprecated!
@Al-Mothafar, I have a small doubt, why did you go beyond when if condition failed. You should exit the answer not proceed.
Thanks, this is the answer that really showed me what I can here to know.
К
Коля Ракета

Parsing string with moment.js.

const date = '1231231231231' //Example String date
const parsed = moment(+date);

This help me get rid of deprecation warning when I was trying to use unix format to get week number.
A
Amina Darwish

Add the following line in your code to Suppress the warnings:

const moment = require('moment');

moment.suppressDeprecationWarnings = true;


That works, but it's like taking out the batteries from a fire alarm
M
Mwongera808

A simple answer:

let date = Date.now();
let timeNow = moment(new Date(date)).format('YYYY-MM-DD');

P
Penny Liu

In my case, this error happens when

moment('2021-07-1')

The correct way is

moment('2021-07-01')

When the month/date is less than 10, you need to add 0 in front.


D
Daisy

I was using moment to convert my date value to the format I want. The date value from the database is like

2021-06-07T22:00:00.000Z

What I did is as below:

dateNeeded = moment(dateNeeded , moment.ISO_8601).format('YYYY-MM-DD');

The reference is here: https://momentjs.com/docs/#/parsing/string-format/


P
Pwntastic

This may be a bit overdue. My issue was I did not have a format at all being passed from the database ( mine was NULL ).

The docs say: 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().


S
Syed Tabish Ali

I have similar issue faced and solve with following solution: my date format is: 'Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)'

let currentDate = moment(new Date('Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)').format('DD-MM-YYYY'); // 'Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)'

let output=(moment(currentDate).isSameOrAfter('07-12-2020'));

K
Keshav Gera
const dateFormat = 'MM-DD-YYYY';

const currentDateStringType = moment(new Date()).format(dateFormat);
    
const currentDate = moment(new Date() ,dateFormat);  // use this 

moment.suppressDeprecationWarnings = true;


S
S_99_chandra
 var nowTime = moment();

 var nowTimeFormate = moment(nowTime).format("HH:mm");

 console.log('from now time',nowTimeFormate)

this syntax help me to avoid warning


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now