Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

Date.prototype.setMonth

Sets the month.

Available since: ES1

Syntax

date.setMonth(monthIndex, day)

Parameters

ParameterTypeDescription
monthIndexnumberThe month from 0 to 11. Values outside the range roll over into another year.
daynumberOptional. 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 15

The end-of-month trap:

const date = new Date(2026, 0, 31);
date.setMonth(1);
 
console.log(date.getMonth() + 1, date.getDate());

Output:

3 3

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.