• JavaScript Video Tutorials

JavaScript Date setMonth() Method



The Date.setMonth() method in JavaScript is used to set the month of a Date object to a specified value, ranging from 0 to 11, where 0 represents January and 11 represents December. This method changes the month component of the date object without altering other components such as the day, year, hour, minute, second, and milliseconds. If the value provided to this method is outside the valid range (0 to 11), the date object's other components will be adjusted accordingly.

Optionally, we can also modify the day value of the date object. If the date of Date object is invalid, this method returns "NaN" as result.

Syntax

Following is the syntax of the JavaScript Date setMonth() method −

setMonth(monthValue, dateValue);

Parameters

This method accepts two parameters. The same is described below −

  • monthValue − An integer between 0 and 11, where 0 is January and 11 is December.
    • If -1 is provided, it will result in the last month of the previous year.
    • If 12 is provided, it will result in the first month of the next year.
  • dateValue (optional) − An integer between 1 and 31.
    • If 0 is provided, it will result in the last day of the previous month.
    • If -1 is provided, it will result in day before the last day of the previous month.
    • If 32 is provided, it will result in the first day of the next month (if that month has 31 days).
    • If 32 is provided, it will result in the second day of the next month (if that month has 30 days).

Return value

This method returns the timestamp representing the adjusted date after setting the new month and optionally the day.

Example 1

In the following example, we are using the JavaScript Date setMonth() method to set the "month" of the Date object to 10 (November) −

<html>
<body>
<script>
   let date = new Date();
   date.setMonth(10); // Sets the month to November

   document.write(date);
</script>
</body>
</html>

Output

If we execute the above program, the month will be set to 10, and the year and day will be according to the local time.

Example 2

Here, we have set the month to 10 (November) and day to (25) −

<html>
<body>
<script>
   let date = new Date();
   date.setMonth(10, 25); // Set month to November, and day to 25

   document.write(date);
</script>
</body>
</html>

Output

After executing, this program returns a timestamp with the provided date.

Example 3

If we provide "12" for a monthValue, the year will incremented by 1 (yearValue + 1) and 0 will be used for the month.

<html>
<body>
<script>
   const date = new Date('2022-11-15'); //December 15 2022
   date.setMonth(12, 15); // It will be January 15 2023

   document.write(date);
</script>
</body>
</html>

Output

It returns a timestamp as "Sun Jan 15 2023 05:30:00 GMT+0530 (India Standard Time)".

Example 4

If we provide "32" for a dateValue, the month will incremented by 1 (if that month has 31 days) and will result in the first day of the next month.

<html>
<body>
<script>
   const date = new Date('2023-10-30'); //October 2023 has 31 days.
   date.setMonth(9, 32); //It will be November 1 2023.

   document.write(date);
</script>
</body>
</html>

Output

It returns a timestamp as "Wed Nov 01 2023 05:30:00 GMT+0530 (India Standard Time)".

Example 5

If we pass a invalid date value as a parameter to this function, the date will be set to "Invalid date" and "NaN" is returned as result −

<html>
<body>
   const date = new Date('2023-10-30'); //October 30 2023
   date.setMonth("asd", "vfdva"); //Invalid date

   document.write(date.getMonth());
</script>
</body>
</html>

Output

As we can see the output, "NaN" is returned as output.

Advertisements