简体   繁体   English

比较两个日期时Moment.js弃用警告

[英]Moment.js deprecation warning when comparing two dates

I didn't see any SO thread that matches my use case. 我没有看到与我的用例匹配的SO线程。 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' . 两个日期的日期格式均为'MM/DD/YYYY'格式,唯一的例外是有时它们可​​能为'MM/DD/YYYY'而其他时候为'M/D/YYYY'

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. 我尝试使用moment.ISO_8601参数,但仍继续获取不建议使用的错误。

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 Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment.js:1弃用警告:提供的值不是公认的RFC2822或ISO格式。 moment construction falls back to js Date(), which is not reliable across all browsers and versions. 此刻的构建工作归结于js Date(),它在所有浏览器和版本之间都不可靠。 Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. 不建议使用非RFC2822 / ISO日期格式,并将在即将发布的主要版本中将其删除。


Is there a clean fix for this? 有没有解决的办法?

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. 无论是'MM/DD/YYYY'也不'M/D/YYYY'是由公认的ISO 8601兼容的格式moment.ISO_8601moment(String)文档,看看有什么是ISO 8601种格式的momentjs认可。 You have to specify 'MM/DD/YYYY' (or 'M/D/YYYY' ) format instead of using moment.ISO_8601 . 您必须指定'MM/DD/YYYY' (或'M/D/YYYY' )格式,而不是使用moment.ISO_8601

You can use both 'MM/DD/YYYY' and 'M/D/YYYY' using moment(String, String[]) . 您可以使用moment(String, String[])使用'MM/DD/YYYY''M/D/YYYY' This is required if you are using strict parsing . 如果您使用严格解析,则这是必需的。 Please note that: 请注意:

Starting in version 2.3.0, Moment uses some simple heuristics to determine which format to use. 从版本2.3.0开始 Moment使用一些简单的试探法来确定要使用的格式。 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, ie prefer stricter parsing. 优先使用比不多解析多的字符串的格式,并优先使用比不多解析的格式,即更严格的解析。
  • Prefer formats earlier in the array than later. 在数组中优先使用格式,而不要在以后使用。

For example, using ['MM/DD/YYYY', 'MM/DD/YYYY'] , ambigous inputs like 01/10/2017 will always be interpreted as 10th of January. 例如,使用['MM/DD/YYYY', 'MM/DD/YYYY'] ,诸如01/10/2017类的模糊输入将始终被解释为一月10日。

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> 

I don't actually see that issue. 我实际上没有看到这个问题。

http://jsfiddle.net/dbkidd/n9s082bj/ http://jsfiddle.net/dbkidd/n9s082bj/

Can you reproduce it in the Fiddle? 你能在小提琴中重现吗?

var start_date = moment().format('MMM DD h:mm A');
var user_start_date = moment().format('MMM DD h:mm A');

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!");
 }

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

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