runoops.com

moment Deprecation warning: value provided is not in a recognized RFC2822 or ISO format

如何处理 moment Deprecation warning: value provided is not in a recognized RFC2822 or ISO format?

当使用 moment 进行时间格式转换时,遇到如下报错:

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 and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.

原代码:

const datetime = '09/06/2023';// 当原时间为非标准格式时(2023年9月6日)
const f5 = moment(datetime).format('YYYY-MM-DD');

解决方法

方法一,增加时间的构造参数(推荐)

const datetime = '09/06/2023';
const f5 = moment(datetime, 'MM/DD/YYYY').format('YYYY-MM-DD');
console.log(f5);//2023-09-06

方法二,通过new Date

const datetime = '09/06/2023';
const f5 = moment(new Date(datetime)).format('YYYY-MM-DD');
console.log(f5);//2023-09-06

方法三,关闭提示

// Suppress the warnings
const moment = require('moment');
moment.suppressDeprecationWarnings = true;

Captcha Code

0 笔记

分享笔记

Inline Feedbacks
View all notes