admin管理员组文章数量:1287505
How does one generally check if a random date is at least one day (24hrs) in the past with momentjs?
Something like:
const today = moment()
const isAtLeastADayAgo = today.subtract(dateToCheck) > 1 // ??
How does one generally check if a random date is at least one day (24hrs) in the past with momentjs?
Something like:
const today = moment()
const isAtLeastADayAgo = today.subtract(dateToCheck) > 1 // ??
Share
Improve this question
asked Feb 13, 2017 at 8:41
j_dj_d
3,08210 gold badges55 silver badges96 bronze badges
3 Answers
Reset to default 10You can simply use 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./ajax/libs/moment.js/2.17.1/moment.min.js"></script>
Or you can use diff
limiting granularity to days:
const today = moment();
const dateToCheck = moment().subtract(3, 'd');
const isAtLeastADayAgo = today.diff(dateToCheck, 'd') > 1;
console.log(isAtLeastADayAgo);
<script src="//cdnjs.cloudflare./ajax/libs/moment.js/2.17.1/moment.min.js"></script>
i would suggest you to do it with plain javascript Date constructor.
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.
const isAtLeastADayAgo = moment().subtract(1,'days')>dateToCheck
本文标签: javascriptHow to check if a date is at least one day in the past with momentjsStack Overflow
版权声明:本文标题:javascript - How to check if a date is at least one day in the past with momentjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234107a2362662.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论