• JavaScript Video Tutorials

JavaScript Date setUTCMonth() Method



The JavaScript Date.setUTCMonth() method is a Date object method that allows you to set the month of a date, according to Coordinated Universal Time (UTC). The return value of this method is the updated timestamp of the Date object, which reflects the changes made by modifying the month. 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 setUTCMonth() method −

setUTCMonth(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 new date after modifying the month and, day (optional).

Example 1

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

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

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

Output

If we execute the above program, the month of the date object will be set to 10.

Example 2

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

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

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

Output

After executing, this program returns an updated timestamp with the provided "month" and "day" values.

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.setUTCMonth(12, 15); // It will be January 15 2023

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

Output

The above program 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>
   const date = new Date('2023-10-30'); //October 2023 has 31 days.
   date.setUTCMonth(9, 32); //It will be November 1 2023.

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

Output

The above program 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.setUTCMonth("asd", "vfdva"); //Invalid date

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

Output

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

Advertisements