我从外部来源获取日期作为输入,它是一个字符串,
const a = '08-01-2019';
对我来说,必填格式为“ MM-DD-YYYY”,
const outputDateFormat = 'MM-DD-YYYY';
我正在使用moment.js并在该日期执行以下操作,例如增加一年和减少一天,
//adding a year
const a = moment(a).add(1,'years').add(-1,'days').format(outputDateFormat);
对于上面的一行,我得到了,
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转换为我的输出所需格式会产生不建议使用的警告。
const finalDate = moment(a).format(outputDateFormat); - Resulting depricated warning
因此,我尝试如下使用新的Date()以避免警告。
//adding a year - approach #1
const a = moment(new Date(a)).add(1,'days').format(outputDateFormat);
这没有返回任何错误,但是我没有考虑使用这种新的Date()方法,因为我的代码应该跨时区和语言环境工作。如果我们使用新的Date()代码,可能无法在时区和区域设置之间正常工作?有人可以建议吗?
因此,我遵循了这种方法2,
//adding a year - approach #2
const a = moment('2019-01-08').add(1,'days').format(outputDateFormat);
我以“ YYYY-MM-DD”的形式给出日期格式,而不是我输入的日期格式“ MM-DD-YYYY”。
这也是,不返回任何警告,并且代码运行正常。这样可以在时区和地区使用吗?
有人可以建议我使用方法1或方法2使我的代码在时区/语言环境中工作,而不会出现任何错误/ momentjs警告吗?
我是否需要在此处使用moment.utc
来使代码跨时区工作?我认为不需要UTC,因为我没有进行任何与时间相关的验证。
我的意思是我不需要矩.utc(),因为我没有时间成分,请提出建议。
moment.utc('2019-01-08').add(1,'days').format(outputDateFormat)
或者其他人可以提出一种比此更好的方法来使代码在时区和语言环境中正常工作吗?