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 232 views
1 answer 227 views
1 answer 218 views
2 answers 256 views
1 answer 256 views
1 answer 215 views
...