比较两个日期并检查给定日期是过去还是相同或未来

Comparing two date and check if given date is in past or same or future

我正在使用 moment.js 来比较两个日期。

const somedayMom = moment(new Date(event.target.value)); // '2021-12-31'
const todayMom = moment();
if (moment(somedayMom).isSameOrAfter(todayMom)) {
            
} else {
            
 }

现在从输入中我得到 '2021-12-31',今天是 12 月 31 日,所以它应该属于 if() 块,但它属于 else 部分。我在这里做错了什么。请帮忙

使用 .format 仅包含年月日。

if (moment(somedayMom.format('YYYY-MM-DD')).isSameOrAfter(todayMom.format('YYYY-MM-DD'))) {
} 
else {
 }

PS。如果 event.target.value returns 正确的字符串,则在您的场景中无需格式化第一时刻。