How to subtract months from a date in TypeScript

1 Answer

0 votes
function subtractMonthsFromDate(months : number, date = new Date()) {
    date.setMonth(date.getMonth() - months);

    return date;
}

const date = new Date('2022-04-18');

console.log(subtractMonthsFromDate(2, date).toDateString());





/*
run:

"Fri Feb 18 2022" 

*/

 



answered Mar 17, 2022 by avibootz

Related questions

1 answer 147 views
1 answer 141 views
1 answer 174 views
2 answers 166 views
1 answer 131 views
1 answer 158 views
...