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 229 views
1 answer 266 views
1 answer 228 views
1 answer 232 views
1 answer 271 views
1 answer 209 views
...