尝试在 moment.js 中转换 RFC2822 日期时,“弃用警告:时刻构造回退到 js 日期" [英] “Deprecation warning: moment construction falls back to js Date” when trying to convert RFC2822 date in moment.js

查看:32
本文介绍了尝试在 moment.js 中转换 RFC2822 日期时,“弃用警告:时刻构造回退到 js 日期"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码使用 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()

但我得到:

弃用警告:moment 构造回退到 js 日期.这是不鼓励的,并将在即将发布的主要版本中删除.请参考 https://github.com/moment/moment/issues/1407更多信息.

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?

推荐答案

要消除警告,您需要:

  • 传入日期字符串的 ISO 格式版本:

  • Pass in an ISO formatted version of your date string:

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

传入您现在拥有的字符串,但告诉 Moment 该字符串的格式:

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

将您的字符串转换为 JavaScript 日期对象,然后将其传递给 Moment:

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

最后一个选项是 Moment 目前支持的内置回退,带有已弃用的控制台警告.他们说他们不会在未来的版本中支持这种回退.他们解释说使用 new Date('my date') 太不可预测了.

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.js 中转换 RFC2822 日期时,“弃用警告:时刻构造回退到 js 日期"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆