How to subtract months from a date in Node.js

1 Answer

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

  	return date;
}

const date = new Date('2022-07-19');

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





/*
run:

Sat Mar 19 2022

*/

 



answered Mar 17, 2022 by avibootz

Related questions

1 answer 143 views
1 answer 132 views
1 answer 126 views
2 answers 129 views
1 answer 151 views
1 answer 120 views
...