发布于 4年前

moment.js警告:Deprecation warning: value provided is not in a recognized RFC2822 or ISO format.

使用moment.js解析日期字符串,检查日期字符串是否为合法有效:

var datestr= '2018-05-01 23:00:00';
if (moment(datestr).isValid()) {
  console.log('valid date');
} else {
  console.log('invalid date');
}

当日期为非法的字符串是,如datestr='2018-05-01 23;00;00',它会在控制台输出警告:

moment.min.js:6 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.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 2018-05-01 23;00:00, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.createFromInputFallback (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:3248)
    at gb (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:19736)
    at rb (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:22824)
    at qb (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:22691)
    at pb (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:22410)
    at sb (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:23150)
    at tb (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:23184)
    at a (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js:6:202)
    at file:///C:/Users/ccs/Desktop/demo.html:60:5

为了避免此警告,可以使用moment(String, String); 第二个参数为日期格式,"2018-05-01 23:00:00"为moment.ISO_8601格式,修改如下:

var datestr= '2018-05-01 23:00:00';
if (moment(datestr,moment.ISO_8601).isValid()) {
  console.log('valid date');
} else {
  console.log('invalid date');
}
©2020 edoou.com   京ICP备16001874号-3