How to check if a date is older than one month in JavaScript

1 Answer

0 votes
function isDateOlderThanOneMonth(date) {
 	const now = new Date();

	const _30DaysInMilliseconds = 30 * 24 * 60 * 60 * 1000;

	const timeDiffInMilliseconds = now.getTime() - date.getTime();

	if (timeDiffInMilliseconds >= _30DaysInMilliseconds) {
    	   return true;
	} else {
    	return false;
    }
}

console.log(isDateOlderThanOneMonth(new Date('2022-3-17')));

console.log(isDateOlderThanOneMonth(new Date('2022-1-15')));

  
  
  
  
/*
run:
  
false
true
  
*/

 



answered Mar 12, 2022 by avibootz
...