如何處理 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 = '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')

相關資料

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容