Javascript 如何在javascript中使用时刻(moment.js)做大于或等于?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27326937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:02:05  来源:igfitidea点击:

How to do a greater than or equal to with moments (moment.js) in javascript?

javascriptdatedatetimemomentjs

提问by johntrepreneur

Basically, I want to do a myMoment >= yourMoment. There is no myMoment.isSameOrAfterand writing that out combining isSameand .isAfteris a bit lengthy.

基本上,我想做一个myMoment >= yourMoment. 没有myMoment.isSameOrAfter和写出来结合起来isSame.isAfter有点冗长。

What's the alternative? Convert moment to js date and use >=to compare?

什么是替代方案?将时刻转换为 js 日期并用于>=比较?

采纳答案by johntrepreneur

Okay, I just went with the moment.js .diff()function.

好的,我刚刚使用了 moment.js.diff()函数。

myMoment.diff(yourMoment) >= 0

myMoment.diff(yourMoment) >= 0

Close enough.

足够近。

回答by Daniel0b1b

You can use the isSameOrAftermethod in momentjs:

您可以isSameOrAfter在 momentjs 中使用该方法:

moment1.isSameOrAfter(moment2);

回答by tuananh

let A = moment('2020-01-02');
let B = moment('2020-01-01');
let C = moment('2020-01-03');
console.log(A.diff(B, 'days'));// => 1
console.log(A.diff(C, 'days'));// => -1

The supported measurements are years, months, weeks, days, hours, minutes, and seconds momentjs.com

支持的度量是年、月、周、天、小时、分钟和秒momentjs.com