弃用警告:moment 构造回退到 js Date

Deprecation warning: moment construction falls back to js Date

我正在尝试转换此日期时间

150423160509 //this is utc datetime

格式如下:

2015-04-24 00:05:09 //local timezone

通过使用 moment.js

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


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

但它给了我这个错误

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

这是我在 Google 中输入 "moment construction falls back to js Date" 时发现的结果。 (来自 Joe Wilson 的 post)

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.

希望对您有所帮助 ;)

您需要告诉 moment 如何解析您的日期格式,如下所示:

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