MomentJS isAfter返回不正确的数据
MomentJS isAfter returning incorrect data
我想知道 YYYY-MM-DD 格式的日期 (2021-11-09) 是否在另一个日期 (2021-11-11) 之后,这应该是错误的,对吗?
console.log(
moment("2021-11-09", "YYYY-MM-DD", true).isAfter(
"2011-11-11",
"YYYY-MM-DD",
true
)
);
但是,上面的代码 return 正确。
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
所以你应该使用一个单位而不是格式字符串,虽然看起来结果是一样的。
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>
您在代码中比较的日期与您问题中的日期不一致。并且代码正确返回 true。因为2021-11-09确实是在2011-11-11
之后
我想知道 YYYY-MM-DD 格式的日期 (2021-11-09) 是否在另一个日期 (2021-11-11) 之后,这应该是错误的,对吗?
console.log(
moment("2021-11-09", "YYYY-MM-DD", true).isAfter(
"2011-11-11",
"YYYY-MM-DD",
true
)
);
但是,上面的代码 return 正确。
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
所以你应该使用一个单位而不是格式字符串,虽然看起来结果是一样的。
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>
您在代码中比较的日期与您问题中的日期不一致。并且代码正确返回 true。因为2021-11-09确实是在2011-11-11
之后