如何处理 moment Deprecation warning: value provided is not in a recognized RFC2822 o

388 阅读1分钟

如何处理 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 momentjs.com/guides/#/wa… for more info.

复现方式:

const datetime = 'abcde`; // 当原时间为非法参数时
  
moment(datetime).format('YYYYMMDD');

解决方法

方法一 (推荐)

增加时间的构造参数

const source_date = '2019-05-06`;
    
moment(source_date, 'YYYY-MM-DD').format('YYYYMMDD');

方法二

关闭提示


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

方法三

关闭提示 不推荐

moment(new Date("27/04/2016")).format('YYYYMMDD')

相关资料

作者:youthcity
链接:www.jianshu.com/p/6ab7c5fb6…
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。