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 239 views
1 answer 223 views
1 answer 263 views
2 answers 307 views
1 answer 224 views
1 answer 255 views
...