gpt4 book ai didi

javascript - 检查时间是否与 Moment.js 相同

转载 作者:行者123 更新时间:2023-11-29 17:51:22 27 4
gpt4 key购买 nike

如何检查具有不同日期的 Moment 对象的时间是否相同?

例如我有这样的对象

const endDate = moment().add(30, 'days').endOf('day');

我想检查某个 moment 对象是否是 endOf 日。

private isEndOfDay(dateTime: string) {
const m = moment().endOf('day');

return m.isSame(dateTime, 'minute');
}

const receivedDateFormat: string = 'YYYY-MM-DD hh:mm:ss';

this.isEndOfDay(this.endDate.format(this.receivedDateFormat))

但是对于这种情况,当我传递“分钟”参数时,它会检查分钟、小时、日、月和年……这不是我想要检查的。

最佳答案

documentation的部分这解释了行为是

When including a second parameter, it will match all units equal or larger. Passing in month will check month and year. Passing in day will check day, month, and year.

所以,如果你只是想比较分钟,你需要做类似的事情

endDate.minute() === startDate.minute()

只比较时间,格式化()日期

endDate.format('HH:mm:ss') === startDate.format('HH:mm:ss')

关于javascript - 检查时间是否与 Moment.js 相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43388367/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com