简体   繁体   English

MomentJS 在返回错误数据后

[英]MomentJS isAfter returning incorrect data

I want to know if a date (2021-11-09) is after another date (2021-11-11) in YYYY-MM-DD format, which should be false, right ?我想知道日期 (2021-11-09) 是否在 YYYY-MM-DD 格式的另一个日期 (2021-11-11) 之后,这应该是假的,对吧?

  console.log(
    moment("2021-11-09", "YYYY-MM-DD", true).isAfter(
      "2011-11-11",
      "YYYY-MM-DD",
      true
    )
  );

but, above code return true.但是,上面的代码返回true。

the doc of momentjs states: momentjs 的文档指出:

If you want to limit the granularity to a unit other than milliseconds, pass the units as the second parameter.如果你想限制的粒度比其他毫秒为单位,通过单位作为第二个参数。

As the second parameter determines the precision , and not just a single value to check, using day will check for year, month and day由于第二个参数决定了 precision ,而不仅仅是要检查的单个值,因此使用 day 将检查年、月和日

So you should use a unit instead of format string though it seems the result is same.因此,尽管结果似乎相同,但您应该使用单位而不是格式字符串。

 console.log( moment("2021-11-09", "YYYY-MM-DD", true).isAfter( "2011-11-11", "day" //"YYYY-MM-DD" ) ) console.log( moment("2021-11-09", "YYYY-MM-DD", true).isAfter( "2021-11-11", "day" //"YYYY-MM-DD" ) )
 <script src="https://unpkg.com/moment@2.29.1/moment.js"></script>

The dates you're comparing in code and the ones in your questions are not aligned.您在代码中比较的日期与问题中的日期不一致。 And code is returning true correctly.并且代码正确返回true。 Because 2021-11-09 is indeed after 2011-11-11因为2021-11-09确实是在2011-11-11之后

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

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