date-manip
Version:
A lightweight JavaScript date utility library that provides modularity, high performance, and additional features. It supports various date operations, including date addition and subtraction, formatting, comparison, etc.
11 lines (10 loc) • 407 B
JavaScript
;
const daysInMonth = require("./daysInMonth.js");
function addMonths(date, months) {
const expectedMonth = date.getMonth() + months;
const tempMaxDay = daysInMonth(new Date(date.getFullYear(), expectedMonth, 1, 0, 0, 0, 0));
const currentDay = date.getDate();
date.setMonth(expectedMonth, currentDay > tempMaxDay ? tempMaxDay : currentDay);
return date;
}
module.exports = addMonths;