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 内部处理逻辑:
- 检查字符串是否与已知的 ISO 8601 格式匹配
- 检查字符串是否与 RFC 2822 日期时间格式匹配
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 中文网