momentjs warning提示

warning提示如下

react_devtools_backend.js:4026 Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 2022-06-09 23:29:07 +0000 UTC
, _f: undefined, _strict: undefined, _locale: [object Object]
Error

提示原因:使用的不是可识别的RFC2822或ISO格式 我使用的是2022-06-09 23:29:07 +0000 UTC

moment 内部处理逻辑

  1. 检查字符串是否与已知的 ISO 8601 格式匹配
  2. 检查字符串是否与 RFC 2822 日期时间格式匹配
  3. new Date(string) 
import moment from 'moment'

const text = '2022-06-09 23:29:07 +0000 UTC'
const format = 'YYYY-MM-DD HH:mm:ss'
const time = moment(text).format(format)


// 解决办法 1 moment解析令牌 http://momentjs.cn/docs/#/parsing/special-formats/
const time1 = moment(text,format).format(format)

// 2 转换时间格式,也就对应了moment处理的第三步
const time2 = moment(new Date(text)).format(format)

// 3 关闭moment warning提示
moment.suppressDeprecationWarnings = true;

参考文档:文档 | Moment.js 中文网

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值