Skip to content

Instantly share code, notes, and snippets.

@heroqu
Created May 16, 2016 09:51
Show Gist options
  • Save heroqu/87416422b3a0ad7e138c239cfbe01730 to your computer and use it in GitHub Desktop.
Save heroqu/87416422b3a0ad7e138c239cfbe01730 to your computer and use it in GitHub Desktop.
Add / subtract number of months from given date (original date in param stays immutable).
function addMonths(date, months) {
var d = new Date(date.getTime());
var m = d.getMonth();
var y = d.getFullYear();
var ym = y * 12 + m;
ym = ym + months;
m = ym % 12;
y = (ym - m) / 12;
d.setYear(y);
d.setMonth(m);
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment