How to get a date of N days ago from current date in Node.js

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(11).toDateString());

  
  
  
  
/*
run:
  
Tue Mar 29 2022
  
*/

 



answered Apr 9, 2022 by avibootz

Related questions

1 answer 125 views
1 answer 132 views
1 answer 140 views
1 answer 168 views
2 answers 171 views
1 answer 121 views
1 answer 164 views
...