addMonths
Date
stable
Return a new Date shifted by the given number of months. To avoid day-overflow issues (e.g. Jan 31 + 1 month), the day-of-month is reset to 1 before advancing the month. The original date is never mutated.
API
Signature
Full TypeScript signature sourced from the util file.
addMonths resets the day to 1 before offsetting the month to avoid overflow (e.g. 31 Jan + 1 month becoming 2 or 3 March). This differs from simple setMonth arithmetic.
API
Parameters
Input parameters accepted by this util.
- daterequiredDate
- The base date. Not mutated.
- amountrequirednumber
- Number of months to add. Negative values subtract months.
API
Return value
The value returned by this util.
- A new Date instance at the 1st day of the target month.
Usage
Examples
Ready-to-paste usage snippets.
Advance a calendar month