How to subtract months from a date in JavaScript

1 Answer

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

  	return date;
}

const date = new Date('2022-03-17');

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





/*
run:

"Mon Jan 17 2022"

*/

 



answered Mar 17, 2022 by avibootz

Related questions

1 answer 233 views
1 answer 266 views
1 answer 230 views
1 answer 235 views
1 answer 274 views
1 answer 211 views
...