Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a date is at least one day in the past with momentjs?

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 // ??
like image 982
j_d Avatar asked Dec 19 '22 09:12

j_d


1 Answers

You 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.com/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.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
like image 81
VincenzoC Avatar answered Dec 20 '22 22:12

VincenzoC