Deprecation warning: Moment construct falls back to js Date

I am trying to convert this datetime

150423160509 // this is a utc datetime

In the following format:

2015-04-24 00: 05: 09 // local time zone

using moment .js

var moment = require('moment-timezone');


var a = moment.tz('150423160509', "Asia/Taipei");
console.log( a.format("YYYY-MM-DD H:m:s") );

      

but it gives me this error

Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release

      

+3


source to share


2 answers


You need to specify the moment how to parse the date format, for example:



var parsedDate = moment.utc("150423160509", "YYMMDDHHmmss");
var a = parsedDate.tz("Asia/Taipei");

// I'm assuming you meant HH:mm:ss here
console.log( a.format("YYYY-MM-DD HH:mm:ss") );

      

+5


source


This is what I discovered when I typed "moment when the construct returns to js Date" on Google. (From a post by Joe Wilson )

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

  • Change to an ISO formatted date string:

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

  • Go to the line you have, but tell Moment what format the line is in:

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

  • Convert your string to JavaScript Date object and pass it to Moment:

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

    The last option is inline fallback that Moment supports, with a console warning alert. They say they will not support this reserve in future releases. They explain that using the new Date ('my date') is too unpredictable.



Hope it helped;)

+4


source







All Articles