Date.prototype.setMonth
Sets the month.
Available since: ES1
Syntax
date.setMonth(monthIndex, day)Parameters
| Parameter | Type | Description |
|---|---|---|
| monthIndex | number | The month from 0 to 11. Values outside the range roll over into another year. |
| day | number | Optional. The day of the month. |
Return value
numberThe new timestamp. The object itself changes.
Examples
Adding three months:
const date = new Date(2026, 10, 15);
date.setMonth(date.getMonth() + 3);
console.log(date.getFullYear(), date.getMonth() + 1, date.getDate());Output:
2027 2 15The end-of-month trap:
const date = new Date(2026, 0, 31);
date.setMonth(1);
console.log(date.getMonth() + 1, date.getDate());Output:
3 3Notes
February has no 31st, so the extra days roll over into March. Before changing the month of a late-in-month date, set the day to 1 or check the result.
See also
- Date.prototype.getMonth — Returns the month — a number from 0 to 11.
- Date.prototype.setDate — Sets the day of the month — handy for adding and subtracting days.
In the tutorial
- Dates and time — The Date object in JavaScript: creating a date, reading the year, month and day, changing a date, the difference between dates and the ISO format.