moment.js:建议弃用:提供的值不是公认的RFC2822或ISO格式 [英] moment.js: Deprecation warning: value provided is not in a recognized RFC2822 or ISO format

查看:472
本文介绍了moment.js:建议弃用:提供的值不是公认的RFC2822或ISO格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从外部来源获取日期作为输入,它是一个字符串,

I am getting date as input from an external source and it is a string,

const a = '08-01-2019';

对我来说,必填格式为"MM-DD-YYYY",

And required format for me is 'MM-DD-YYYY',

const outputDateFormat = 'MM-DD-YYYY';

我正在使用moment.js并在该日期执行以下操作,例如增加一年和减少一天,

And I am using moment.js and doing below manipulations on that date like adding a year and decreasing a day,

//adding a year 

const a = moment(a).add(1, 'years').add(-1, 'days').format(outputDateFormat);

对于上面的一行,我得到了,

for the above line, I am getting ,

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.

我的意思是,使用moment转换为我的输出所需格式会产生不建议使用的警告.

I mean, Converting to my output required format using moment is giving the deprecated warning.

const finalDate = moment(a).format(outputDateFormat); - Resulting depricated warning

因此,我尝试如下使用新的Date()以避免警告.

So, I have tried using new Date() as below to avoid warnings.

//adding a year - approach #1

    const a = moment(new Date(a)).add(1, 'years').add(-1, 'days').format(outputDateFormat);

这没有返回任何错误,但是我没有考虑使用这种新的Date()方法,因为我的代码应该跨时区和语言环境工作.如果我们使用新的Date()代码,可能无法在时区和语言环境之间正常工作?有人可以建议吗?

This is not returning any error but I am not considering this approach of new Date() as my code should work across timezones and locales. And if we use new Date() code might not work properly across timezones and locales? can anyone suggest?

所以,我遵循了#2的方法,

And so, I have followed this approach # 2,

//adding a year - approach #2

    const a = moment('2019-01-08').add(1, 'years').add(-1, 'days').format(outputDateFormat);

我以"YYYY-MM-DD"的形式给出日期格式,而不是我输入的日期格式"MM-DD-YYYY".

Where I am giving date format in the form 'YYYY-MM-DD' instead of my input date format 'MM-DD-YYYY'.

这也是,不返回任何警告,并且代码运行正常.这样可以在时区和地区使用吗?

This is also, not returning any warning and code is working fine. Will this work across timezones and locales?

有人可以建议我使用方法#1或#2使我的代码在时区/语言环境中正常工作,而不会出现任何错误/momentjs警告吗?

Can anyone suggest me to use approach #1 or #2 to make my code work across timezones/locales without any errors/momentjs warnings?

我需要在此处使用 moment.utc 来使代码跨时区工作吗?我认为不需要UTC,因为我没有进行任何与时间相关的验证.

Do I need to use moment.utc here to make code work across timezones? I think UTC is not required, As I am not having any time-related validations.

我的意思是我不需要矩.utc(),因为我没有时间成分,请提出建议.

I mean moment.utc() is not required I guess as there is no time component for me, please suggest.

moment.utc('2019-01-08').add(1, 'years').add(-1, 'days').format(outputDateFormat)

或者其他人可以提出一种比此更好的方法来使代码在时区和语言环境中正常工作吗?

Or else can anyone suggest a better approach than this to make code work across timezones and locales without any issues?

推荐答案

构造新的矩对象时,您需要指定输入的格式

You need to specify the format of the input when construct new moment object

const moment = require('moment')

const a = '08-01-2019';
const outputDateFormat = 'MM-DD-YYYY';

const b = moment(a, 'MM-DD-YYYY').add(1, 'years').add(-1, 'days').format(outputDateFormat);
console.log(b);

这篇关于moment.js:建议弃用:提供的值不是公认的RFC2822或ISO格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆