Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

“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?

like image 489
dariush Avatar asked Apr 24 '14 08:04

dariush


People also ask

Why is moment deprecated JS?

Moment construction falls back to js Date. This is discouraged and will be removed in an upcoming major release. This deprecation warning is thrown when no known format is found for a date passed into the string constructor.

How do I change a moment date to a specific format?

Date Formatting Date format conversion with Moment is simple, as shown in the following example. moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format.

What can I use instead of moment in JavaScript?

There are several libraries out there that can potentially replace Moment in your app. The creators of Moment recommend looking into Luxon, Day. js, date-fns, js-Joda, or even replacing Moment with native JS.

What is Moment date in JavaScript?

Moment's format() method is what you use to convert a Moment object to a string. For example, here's how you would convert a YYYY-MM-DD string into a more human-readable format: const moment = require('moment'); const d = new Date('2019/06/01'); moment(d).format('MMMM d, YYYY'); // June 1, 2019.


4 Answers

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.

like image 119
Joe Wilson Avatar answered Oct 17 '22 06:10

Joe Wilson


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

like image 30
niutech Avatar answered Oct 17 '22 08:10

niutech


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,

  1. 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");

  1. 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

  1. As suggested by @niutech in previous answer, set

moment.suppressDeprecationWarnings = true;

  1. 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.

like image 20
Vignesh Raja Avatar answered Oct 17 '22 07:10

Vignesh Raja


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'}}
like image 33
Fergus Avatar answered Oct 17 '22 06:10

Fergus