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 231 views
1 answer 217 views
1 answer 254 views
2 answers 300 views
1 answer 216 views
1 answer 249 views
...