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 130 views
1 answer 139 views
1 answer 149 views
1 answer 176 views
2 answers 178 views
1 answer 131 views
1 answer 168 views
...