How to get a date of N days ago from specific date in TypeScript

1 Answer

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

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

  	return dateAgo;
}


console.log(getDateOfNDaysAgo(4, new Date('2022-03-11')).toDateString());

  
  
  
  
/*
run:
  
"Mon Mar 07 2022" 
  
*/

 



answered Apr 9, 2022 by avibootz
...