Date.prototype.setMonth() changes the month of an existing Date in local time. JavaScript months are zero-based—January is 0 and December is 11—and the optional second argument can set the day of the month at the same time.
Syntax:
date.setMonth(monthValue);
date.setMonth(monthValue, dateValue);
For example, month value 3 means April:
const date = new Date(2026, 0, 10);
date.setMonth(3);
console.log(date.toDateString()); // Fri Apr 10 2026
| Value | Month | Value | Month |
|---|---|---|---|
| 0 | January | 6 | July |
| 1 | February | 7 | August |
| 2 | March | 8 | September |
| 3 | April | 9 | October |
| 4 | May | 10 | November |
| 5 | June | 11 | December |
The original page used setMonth(3, 5). This sets the month to April and the day to 5:
const date = new Date(2026, 0, 20);
date.setMonth(3, 5);
console.log(date.toDateString()); // Sun Apr 05 2026
Month 12 rolls into January of the next year. Negative months roll into the previous year.
const date = new Date(2026, 5, 5);
date.setMonth(12, 5);
console.log(date.toDateString()); // Tue Jan 05 2027
const date = new Date(2026, 8, 10);
date.setMonth(date.getMonth() + 4);
console.log(date.toDateString()); // Sun Jan 10 2027
The current day-of-month participates in the calculation. Changing 31 January directly to February can overflow because February has fewer days.
const date = new Date(2026, 0, 31);
date.setMonth(1);
console.log(date.toDateString()); // rolls into March
If your rule means “same day if possible, otherwise the last valid day,” calculate the destination month’s last day and clamp the original day.
function setMonthClamped(date, monthValue) {{
const copy = new Date(date);
const wantedDay = copy.getDate();
copy.setDate(1);
copy.setMonth(monthValue);
const lastDay = new Date(copy.getFullYear(), copy.getMonth() + 1, 0).getDate();
copy.setDate(Math.min(wantedDay, lastDay));
return copy;
}}
setMonth() mutates the Date and returns the new timestamp as a number.
const date = new Date();
date.setUTCMonth(0, 1);
console.log(date.toISOString());
Next: setFullYear() Date Reference
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.