ChatGPT解决这个技术问题 Extra ChatGPT

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

I am using the following code to convert a server-side date-time to local time using moment.js.

 moment(moment('Wed, 23 Apr 2014 09:54:51 +0000').format('lll')).fromNow()

But I am getting:

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.

It seems I cannot get rid of it! How can I fix it?

did you read where its says to do so?
Of course i read!! but either i am confused and couldn't fetch any solution or there wasn't any solution in there at all!
After reading a bit there, it seems to be a real issue, which was discussed months ago...Take another tool...

J
Joe Wilson

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.


Any of the text in the bullets in the above answer would make a much better deprecation warning message than the confusing one that started this question.
Is using moment((new Date('Thursday, November 12, 2015 11:59 PM').toISOSring()); ok? Or am I just suppressing a problem ready to silently crash in the near future like this?
You can also have this message if the value passed contains leading or trailing extra characters, such as spaces. It generally goes with a Invalid date result.
@BrunoFinger that should work. Date() and toISOString() are JavaScript things, not Moment things.
If you don't have a choice on how the date is provided, I'd recommend the second bullet point, specifying the format of the date String. It removes the most ambiguity.
n
niutech

As an alternative, you can suppress showing the deprecation warning by setting moment.suppressDeprecationWarnings = true;


For TypeScript, you may have to precede this with // @ts-ignore (at least for some versions of moment-timezone and @types/moment-timezone).
Thanks, I will suppress the warning on my tests. I'm just trying to test the wrong input scenario, it's bothering me and only you can resolve it :)
C
Community

The date construction in moment internally uses the new Date() in the javascript. The new Date() construction recognizes the date string in either RFC2822 or ISO formats in all browsers. When constructing a moment object with date not in these formats, the deprecation warning is thrown.

Though the deprecation warnings are thrown, for some formats, the moment object will be successfully constructed in Chrome, but not in Firefox or Safari. Due to this, processing the date in Chrome may give results as expected(not all the time) and throws Invalid Date in others.

Consider, 02.02.2018,

Chrome - moment("02.02.2018")._d -> Fri Feb 02 2018 00:00:00 GMT+0530 (India Standard Time)

Firefox - moment("02.02.2018")._d -> Invalid Date

Safari - moment("02.02.2018")._d -> Invalid Date

So the moment.js is used at your own risk in case the recommended/standard formats are not used.

To suppress the deprecation warnings,

As suggested by @Joe Wilson in previous answer, give the date format on moment construction.

Example : moment("02.05.2018", "DD.MM.YYYY").format("DD MM YYYY");

Give the date in ISO or RFC2822 format.

Example : moment("2018-02-01T18:30:00.000Z") - ISO Format

moment("Thu, 01 Feb 2018 18:30:00 GMT") - RFC2822 Format - Format in Github

As suggested by @niutech in previous answer, set

moment.suppressDeprecationWarnings = true;

I suggest to overwrite the input fallback in moment. moment.createFromInputFallback=function (config){ config._d = new Date(config._i); }

As (3) will suppress all the warnings, (4) will suppress only the date construction fallback. Using (4), you will get Invalid Date as the internal new Date() is used and other deprecations can be seen in console, so moment can be upgraded or the deprecated methods can be replaced in the application.


F
Fergus

If your date is passed to you from an API as string(like my issue), you can use a filter to convert the string to a date for moment. This will take care of the moment construction warning.

$scope.apiDate = 10/29/2017 18:28:03";

angular.module('myApp').filter('stringToDate', function() {
  return function(value) {
     return Date.parse(value);
  };  
});

Add it to the view:

{{apiDate | stringToDate | amDateFormat:'ddd, MMM DD'}}

u
user2027058

As indicated in the above answers. Providing the date format should work.

Why would I be getting the deprecation message with the following line of code. I thought the String + format was suppose to remedy the issue. moment.tz('2015:08:20 14:33:20', 'YYYY:MM:DD HH:mm:ss', 'America/New_York'). Also, please not I do not have control over the date format being provide. I know I can convert it myself to 'YYYY-MM-DDTHH:mm:ss' then moment does not show the deprecation message. However, according to the documentation, the line of code should work. Here is the deprecation message I am seeing.

"Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/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."


d
david

In my case I was trying to generate a date time to include in my form data. But the format of the string I had access to looked like "10-Sep-2020 10:10" so when trying to use moment like

myDate = '10-Sep-2020 10:10';

moment(myDate).format('YYYY-MM-DD HH:mm:ss');

I got the deprecation warning. There is no problem using a string to create the date but you just have to let moment know what it is you are passing in. As in explicitly state the format it is about to receive, for example

moment(myDate, 'DD-MMM-YYYY HH:mm').format('YYYY-MM-DD HH:mm:ss');

result: 2020-09-10 10:10:00 That's it, the warning goes away, moment is happy and you have a date time format ready for persistence.


When answering an old question having an accepted answer (look for the green ✓) as well as other answers ensure your answer adds something new or is otherwise helpful in relation to them. As such the accepted answer from Joe Wilson states Pass in the string you have now, but tell Moment what format the string is in <code example> basically already gives the same answer.
P
Panayiotis Georgiou

Moment’s usage is so widespread that it’s impossible to deprecate the current version over time. Check out this post on alternative options Migrating away from moment.js


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

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now