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.
12 lines (11 loc) • 396 B
JavaScript
import daysInMonth from "./daysInMonth.mjs";
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;
}
export {
addMonths as default
};