比较两个日期时的 Moment.js 弃用警告 [英] Moment.js deprecation warning when comparing two dates

查看:56
本文介绍了比较两个日期时的 Moment.js 弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有看到任何符合我的用例的 SO 线程.我正在尝试比较两个日期以查看它们是否匹配.如果他们不这样做,那么我会显示一条错误消息.两个日期的日期格式都是 'MM/DD/YYYY' 格式,唯一的例外是有时它们可​​能是 'MM/DD/YYYY' 和其他时间'M/D/YYYY'.

I didn't see any SO thread that matches my use case. I'm trying to compare two dates to see if they match or not. If they don't, then I display an error message. The date format for both dates are in 'MM/DD/YYYY' format, with the only exception being sometimes they might be 'MM/DD/YYYY' and other times 'M/D/YYYY'.

我能够让它工作,但不断收到不推荐使用的错误.我尝试使用 moment.ISO_8601 参数,但仍然不断收到已弃用的错误.

I'm able to get it to work but keep getting a deprecated error. I tried using the moment.ISO_8601 argument but still keep getting the deprecated error.

if( !moment( start_date, moment.ISO_8601 ).isSame( user_start_date, 
  moment.ISO_8601 ) )
{
    console.log("Both dates must match!");
}
else {
    console.log("Dates match!");
}

不推荐使用的错误如下:

The deprecated error is following:

moment.js:1 弃用警告:提供的值不是公认的 RFC2822 或 ISO 格式.moment 构造回退到 js Date(),这在所有浏览器和版本中都不可靠.不鼓励使用非 RFC2822/ISO 日期格式,并将在即将发布的主要版本中删除.

moment.js:1 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.

<小时>

有没有干净的解决方法?


Is there a clean fix for this?

推荐答案

'MM/DD/YYYY''M/D/YYYY' 都不是 ISO 8601moment.ISO_8601 识别的兼容格式,请参见moment(String) 文档以了解什么是 ISOmomentjs 识别的 8601 格式.您必须指定 'MM/DD/YYYY'(或 'M/D/YYYY')格式而不是使用 moment.ISO_8601.

Neither 'MM/DD/YYYY' nor 'M/D/YYYY' are ISO 8601 compliant format recognized by moment.ISO_8601 See moment(String) docs to see what are ISO 8601 formats recognized by momentjs. You have to specify 'MM/DD/YYYY' (or 'M/D/YYYY') format instead of using moment.ISO_8601.

您可以使用 moment(String, String[]).如果您使用严格解析,则这是必需的.请注意:

You can use both 'MM/DD/YYYY' and 'M/D/YYYY' using moment(String, String[]). This is required if you are using strict parsing. Please note that:

2.3.0 版开始, Moment 使用一些简单的启发式方法来确定要使用的格式.按顺序:

Starting in version 2.3.0, Moment uses some simple heuristics to determine which format to use. In order:

  • 优先使用导致有效日期而非无效日期的格式.立>
  • 更喜欢解析更多字符串而不是更少并且使用更多格式而不是更少的格式,即更喜欢更严格的解析.
  • 优先选择数组中较早的格式.
  • Prefer formats resulting in valid dates over invalid ones.
  • Prefer formats that parse more of the string than less and use more of the format than less, i.e. prefer stricter parsing.
  • Prefer formats earlier in the array than later.

例如,使用['MM/DD/YYYY', 'MM/DD/YYYY'],模糊输入如01/10/2017将始终解释为 1 月 10 日.

For example, using ['MM/DD/YYYY', 'MM/DD/YYYY'], ambigous inputs like 01/10/2017 will always be interpreted as 10th of January.

这里有一个没有弃用警告的例子:

Here an example that do not has Deprecation warning:

var start_date = '11/25/2017';
var user_start_date = '27/12/2017';

if( !moment( start_date, ['MM/DD/YYYY', 'M/D/YYYY']  ).isSame( moment(user_start_date, ['MM/DD/YYYY', 'M/D/YYYY'] )) )
{
    console.log("Both dates must match!");
}
else{
    console.log("Dates match!");
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>

这篇关于比较两个日期时的 Moment.js 弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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