How to get a date of N days ago from current date in JavaScript

1 Answer

0 votes
function getDateOfNDaysAgo(days, date = new Date()) {
  	const dateAgo = new Date(date.getTime());

  	dateAgo.setDate(date.getDate() - days);

  	return dateAgo;
}


console.log(getDateOfNDaysAgo(5).toDateString());

  
  
  
  
/*
run:
  
"Mon Apr 04 2022"
  
*/

 



answered Apr 9, 2022 by avibootz
...