How to get a date of N days ago from current 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).toDateString());
 
   
   
   
   
/*
run:
   
"Tue Apr 05 2022" 
   
*/

 



answered Apr 9, 2022 by avibootz
...