Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  150] [ 3]  / answers: 1 / hits: 108572  / 10 Years ago, thu, april 24, 2014, 12:00:00

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?


More From » date

 Answers
41

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.


[#71322] Tuesday, April 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;