JavaScript setMonth

The JavaScript setMonth function is one of the Date Functions, which is used to set the Month number and Day Number of a given date as per the local time zone.

The syntax of the setMonth function is:

 Date.setMonth(Month_Number, Day_Number)

Parameters: The JavaScript setMonth() method accepts two parameter values.

  • Month_number: It accepts an integer month number from 0 (January) to 11 (December) to add months to the given date.
  • Day_Number (Optional): It accepts an integer value ranging from 0 to 31 as the day of the month.

Return Value: It updates the original date with user given month number and day of the month. If we pass NaN or any invalid dates, the setMonth() method returns NaN.

JavaScript setMonth example

In the following example, we use the setMonth() function to set the current month to April. Remember, Month_Number should be between 0 and 11, where 0 is January, 1 is February, 2 is March, and 11 is December.

const dt = new Date();
console.log(dt);
dt.setMonth(3);
console.log(dt);
Mon Jun 15 2026 12:54:05 GMT+0530 (India Standard Time) 
Wed Apr 15 2026 12:54:05 GMT+0530 (India Standard Time)

Example 2: In this JavaScript example, we set the month number of a custom date to 11 (December). Here, we parse the string date time and use the JavaScript setMonth() method to set the custom date month to December.

const dt = new Date("April 1, 2025 10:11:22");
console.log(dt);
dt.setMonth(11);
console.log(dt);
Tue Apr 01 2025 10:11:22 GMT+0530 (India Standard Time) 
Mon Dec 01 2025 10:11:22 GMT+0530 (India Standard Time)

Example 3: In this setMonth example, we use the Date() object to parse a string date without a month or day number. The Date() method sets the custom date month value as 1st January. Next, the setMonth() sets the month number to 05, which is June.

const dt = new Date("2026 10:11:22");
console.log(dt);
dt.setMonth(5);
console.log(dt);
Thu Jan 01 2026 10:11:22 GMT+0530 (India Standard Time) 
Mon Jun 01 2026 10:11:22 GMT+0530 (India Standard Time)

Example 4: As we mentioned earlier, the day of the month number is an optional argument of the setMonth function. If you want to update the date to January 1, then use dt.setMonth(0, 1).

const dt = new Date();
console.log(dt);
dt.setMonth(0, 1);
console.log(dt);
Mon Jun 15 2026 13:05:41 GMT+0530 (India Standard Time) 
Thu Jan 01 2026 13:05:41 GMT+0530 (India Standard Time)

Example 5: Using JavaScript setMonth with a negative month number

If we use the setMonth() method with a negative parameter (month), it returns the previous year’s values. For instance, setMonth(1-) means the last year’s last month. If it is -3, the date becomes October 2025.

const dt = new Date();
console.log(dt);
dt.setMonth(-1);
console.log(dt);
Mon Jun 15 2026 12:56:37 GMT+0530 (India Standard Time) 
Mon Dec 15 2025 12:56:37 GMT+0530 (India Standard Time)

Example 6: If we use the setMonth with a month number between 0 and 11, it updates the existing date’s month only. However, if we exceed that number, it updates the year value.

In the following example, we use the JavaScript setMonth with 15 as the month number. As we know, 12 months = 1 Year, so it updates the current year to next year and the month number to 3, which represents April.

const dt = new Date();
console.log(dt);
dt.setMonth(15);
console.log(dt);
Mon Jun 15 2026 13:07:21 GMT+0530 (India Standard Time) 
Thu Apr 15 2027 13:07:21 GMT+0530 (India Standard Time)

Example 7: Similar to the above, if we use above 31 as the day of the month value, it updates the month number.

In the example below, setMonth(11, 45) means set the month number to December. However, 45 means 31 days of December + an extra 14 days. So, the setMonth() updates the Month from December to January and sets the day of the month to 14.

const dt = new Date();
console.log(dt);
dt.setMonth(11, 45);
console.log(dt);
Mon Jun 15 2026 13:10:57 GMT+0530 (India Standard Time) 
Thu Jan 14 2027 13:10:57 GMT+0530 (India Standard Time)

Why does JavaScript setMonth() skip or give the wrong month?

BY default, the setMonth() function updates the month number based on the given argument. If you pass only one argument, there are issues with the 30 and 31-day months.

For example, the existing date is January 31, 2026, and we want to update it to February. If we use setMonth(1), it tries to set it to February 31st, which does not exist. Here, 31 = (February 28 days + 3 extra days), and those three extra days are moved to the next month (March).

TIP: If you try to setMonth(4), as April has 30 days, it moves to the next month (May 1st).

const dt = new Date("2026-01-31");
console.log(dt);
dt.setMonth(1);
console.log(dt);
Sat Jan 31 2026 05:30:00 GMT+0530 (India Standard Time) 
Tue Mar 03 2026 05:30:00 GMT+0530 (India Standard Time)

Fix: Always use the second parameter and set the day of the month number.

const dt = new Date("2026-01-31");
console.log(dt);
dt.setMonth(3, 30);
console.log(dt);
Sat Jan 31 2026 05:30:00 GMT+0530 (India Standard Time) 
Thu Apr 30 2026 05:30:00 GMT+0530 (India Standard Time)

How to add or subtract months from a Date object using setMonth?

We can use the combination of the JavaScript setMonth() and getMonth() methods to add or subtract months from the current date and time or a custom datetime.

In the following example, we add 5 months to the current date and subtract 12 months from it. Here, the current month is June, and its number is 5. SO, 5 + 5 = 10, which is November.

const dt = new Date();
console.log(dt);
dt.setMonth(dt.getMonth() + 5);
console.log(dt);

dt.setMonth(dt.getMonth() - 12);
console.log(dt);

Mon Jun 15 2026 13:24:17 GMT+0530 (India Standard Time) 
Sun Nov 15 2026 13:24:17 GMT+0530 (India Standard Time) 
Sat Nov 15 2025 13:24:17 GMT+0530 (India Standard Time)