Home > database >  MomentJS isAfter returning incorrect data
MomentJS isAfter returning incorrect data

Time:11-13

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 ?

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

but, above code return true.

CodePudding user response:

the doc of momentjs states:

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

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/[email protected]/moment.js"></script>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

The dates you're comparing in code and the ones in your questions are not aligned. And code is returning true correctly. Because 2021-11-09 is indeed after 2011-11-11

  • Related