如何通过momentjs检查过去的随机日期是否至少为一天(24小时)?
类似的东西:
const today = moment()
const isAtLeastADayAgo = today.subtract(dateToCheck) > 1 // ??
答案 0 :(得分:5)
您只需使用isBefore
function isADayAgo(input){
let yesterday = moment().subtract(1, 'd');
return input.isBefore(yesterday);
}
const isAtLeastADayAgo = isADayAgo(moment());
console.log(isAtLeastADayAgo);

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
&#13;
或者您可以将diff
限制粒度用于日期:
const today = moment();
const dateToCheck = moment().subtract(3, 'd');
const isAtLeastADayAgo = today.diff(dateToCheck, 'd') > 1;
console.log(isAtLeastADayAgo);
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
&#13;
答案 1 :(得分:1)
我建议你使用普通的javascript Date构造函数。
var today = new Date();
var pastDate = // some past date
// 86400 seconds in 24hrs
// getTime() will return you date in milliseconds
if(86400000 < today.getTime()-pastDate.getTime()) will return true if past date is older than 24Hrs.
答案 2 :(得分:0)
const isAtLeastADayAgo = moment().subtract(1,'days')>dateToCheck