JavaScript setMonth(): Set or Change the Month

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

JavaScript Month Numbers Are Zero-Based Top ↑

ValueMonthValueMonth
0January6July
1February7August
2March8September
3April9October
4May10November
5June11December

Setting Month and Day Together Top ↑

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

Overflow and Negative Month Values Top ↑

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

Adding or Subtracting Months Top ↑

const date = new Date(2026, 8, 10);
date.setMonth(date.getMonth() + 4);
console.log(date.toDateString()); // Sun Jan 10 2027

The End-of-Month Pitfall Top ↑

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;
}}

Return Value and Mutation Top ↑

setMonth() mutates the Date and returns the new timestamp as a number.

Using setUTCMonth() Top ↑

const date = new Date();
date.setUTCMonth(0, 1);
console.log(date.toISOString());

Common setMonth() Mistakes Top ↑

  • Using 1 for January instead of 0.
  • Ignoring the current day-of-month when switching from a 29th, 30th or 31st to a shorter month.
  • Expecting a new Date object rather than mutation.
  • Using local setters when the requirement is UTC.

Next: setFullYear() Date Reference




Subscribe to our YouTube Channel here



plus2net.com










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer