How to get a date of N days ago from specific 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, new Date('2022-03-12')).toDateString());

  
  
  
  
/*
run:
  
Tue Mar 01 2022
  
*/

 



answered Apr 9, 2022 by avibootz

Related questions

1 answer 126 views
1 answer 112 views
1 answer 154 views
1 answer 126 views
1 answer 136 views
...