Skip to main content
@spa-tools/utilities

addMonths()

The addMonths() function adds a number of months to a given date object, timestamp integer, or date string.

Usage

import { addMonths } from '@spa-tools/utilities';

const date = new Date('2021-01-01');

const newDate1 = addMonths(date, 5);
console.log(newDate1.toLocaleDateString('en-US'));
// --> '06/01/2021'

const newDate2 = addMonths(date.getTime(), 6);
console.log(newDate2.toLocaleDateString('en-US'));
// --> '07/01/2021'

const newDate3 = addMonths('2021-01-01', 7);
console.log(newDate3.toLocaleDateString('en-US'));
// --> '08/01/2021'

Arguments

NameTypeRequired?Description
datestring | number | DateyesThe date value to add months to as either date string, timestamp integer, or date object
monthsnumberyesThe number of months to add

Returns

Date - The new date object with the added months.