You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MomentJS isSameOrAfter判断计划状态不符合预期问题求助

问题原因及解决方案

原因分析

你当前的代码中,moment()生成的today包含精确到毫秒的当前时间,而planEndDate是当天11:00:00。isSameOrAfter()方法默认会精确到毫秒做全量时间比较,11:00:00早于15:03:04,因此返回false,触发else分支得到inactive状态,这和你只关注日期是否相同的预期不符。

解决方案

要实现“结束日期等于或晚于今日日期(仅比较年月日,忽略时分秒)”的逻辑,有两种常见处理方式:

方式1:指定比较精度为day

在isSameOrAfter方法中传入第二个参数'day',让moment仅按日期维度比较:

const today = moment();
const planEndDate = moment(value);
let content;
if (planEndDate.isSameOrAfter(today, 'day')) {
    content = renderNotification('info', 'active');
} else {
    content = renderNotification('warning', 'inactive');
}

方式2:统一转换为当日起始时间

将两个日期都转为当天00:00:00的时间点,再做比较:

const today = moment().startOf('day');
const planEndDate = moment(value).startOf('day');
let content;
if (planEndDate.isSameOrAfter(today)) {
    content = renderNotification('info', 'active');
} else {
    content = renderNotification('warning', 'inactive');
}

内容的提问来源于stack exchange,提问作者Jeremy Chee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 22:52:04