JSFiddle - React, Tailwind, and code Playground
JavaScript
// if 29, 30 or 31 turned into 1, 2, or 3 then eliminate the overflow
// Day of Month is NOT zero-indexed so .setDate(0) is last day of prior month
function addMonthsNoOverflow(dateParam, intParam) {
var sum = new Date(new Date(dateParam.getTime()).setMonth(dateParam.getMonth() + intParam));
if (sum.getDate() < dateParam.getDate()) { sum.setDate(0); }
return(sum);
}
var d1 = new Date(); d1.setMonth(7); d1.setDate(30);
alert(d1+"plus 6 months is: " + addMonthsNoOverflow(d1, 6)+" without overflow.");