简体   繁体   English

以字符串格式提供时刻时的 ISO 格式弃用警告

[英]ISO format deprecation warning when providing moment in string format

const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" (dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days").format(DATE_FORMAT);
const currentDate = moment().startOf("day").format(DATE_FORMAT);
const validDate = moment(currentDate).isSameOrBefore(appValidDate);

I have been trying to compare two dates using moment.我一直在尝试使用 moment 来比较两个日期。 While running the application, i am getting the below deprecation 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.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Invalid date, _f: undefined, _strict: undefined, _locale: [object Object]
Error:

Found some usefult stackoverflow links: Moment.js deprecation warning when comparing two dates发现了一些有用的 stackoverflow 链接: Moment.js deprecation warning when comparison two dates

But still not able to remove the deprecation warning.但仍然无法删除弃用警告。

So as per the doc, need to be in string + format, so i have done like this:所以根据文档,需要采用字符串+格式,所以我这样做了:

const DATE_FORMAT = "YYYY-MM-DD";
const endDate = "2020-05-05T00:00:00.000Z" (dynamic value from service)
const appValidDate = moment(endDate).subtract(1, "days").format(DATE_FORMAT);
const currentDate = moment().startOf("day").format(DATE_FORMAT);
const validDate = moment(currentDate, DATE_FORMAT).isSameOrBefore(appValidDate);

But the problem is we can't convert endDate to string & then subtract the days.但问题是我们不能将 endDate 转换为字符串然后减去天数。 If i am passing like that, getting Moment error.如果我这样通过,就会出现 Moment 错误。

Can anybody help me to find a proper solution for this.谁能帮我找到一个合适的解决方案。 Any help would be really appreciated.任何帮助将非常感激。

As explained in the comments above, do your date comparison using moment instances.如上面评论中所述,使用moment实例进行日期比较。

The value returned by .format() is a string and depending on the format chosen (and possibly your locale) may trigger the warning you're seeing. .format()返回的值是一个字符串,根据选择的格式(可能还有您的语言环境)可能会触发您看到的警告。

Use .format() when you want to display a value.如果要显示值,请使用.format()

 const DATE_FORMAT = "YYYY-MM-DD"; const endDate = "2020-05-05T00:00:00.000Z" //(dynamic value from service) const appValidDate = moment(endDate).subtract(1, "days"); const currentDate = moment().startOf("day"); // or for a UTC "start of day" // const currentDate = moment.utc().startOf('day') const validDate = currentDate.isSameOrBefore(appValidDate); console.log('appValidDate:', appValidDate.format(DATE_FORMAT)) console.log('currentDate:', currentDate.format(DATE_FORMAT)) console.log('validDate:', validDate)
 <script src="https://momentjs.com/downloads/moment.js"></script>

I was getting the same issue, I used below snippet, it worked for me我遇到了同样的问题,我在下面使用了代码片段,它对我有用

moment(date, moment.ISO_8601)

Thank You谢谢你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM