其他分享
首页 > 其他分享> > 关于Moment.js warning:Deprecation warning: value provided is not in a recognized RFC2822 or ISO format

关于Moment.js warning:Deprecation warning: value provided is not in a recognized RFC2822 or ISO format

作者:互联网

关于Moment.js 报错


近期在接手一个项目时,发现原代码运行时会报一个关于Moment.js的warning。

Warning信息:

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.

通过信息大概知道warning的意思是:所提供的值并不是被识别的RFC822或ISO格式。
相信有小伙伴在做项目时会遇到这个warning,虽然这个warning并不是自己写出来的,但既然看到了那就处理一下。

错误代码:

                if (typeof a === 'string') {
                    const aDate = moment(a), bDate = moment(b);
                    if (aDate.isValid() && bDate.isValid()) {
                        return balbabla
                    } else {
                        return balbabla
                    }
                } else {
                    return balbabla
                }

解决方案

const aDate = moment(a), bDate = moment(b);

改作下面任意一句

const aDate = moment(a,moment.ISO_8601), bDate = moment(b,moment.ISO_8601);
const aDate = moment(a,moment.RFC_2822), bDate = moment(b,moment.RFC_2822);

这样value就以正确且可被识别的格式提供并使用了,便不会再报关于format的warning

标签:Deprecation,bDate,recognized,js,moment,warning,ISO,aDate
来源: https://blog.csdn.net/qq_43263320/article/details/117527761