The setUTCMonth function in JavaScript is utilized to assign an integer that denotes the month for a given date, using Coordinated Universal Time (UTC). The integer values for the setUTCMonth method commence at 0, which corresponds to January.
Syntax
The syntax for the setUTCMonth method is illustrated as follows:
dateObj.setUTCMonth(monthValue[, dayValue])
Parameter
monthValue - This denotes an integer ranging from 0 to 11 that indicates the month.
dayValue - This parameter is optional. It denotes an integer ranging from 1 to 31 that indicates the specific day of the month.
JavaScript Date setUTCMonth method example
In this section, we will explore the setUTCMonth method by examining a variety of examples.
Example 1
Let us examine an example that demonstrates how to display both the current month and the updated month.
<script>
var month=new Date();
document.writeln("Current month : "+(month.getUTCMonth()+1)+"<br>");
month.setUTCMonth(8);
document.writeln("Updated month : "+(month.getUTCMonth()+1));
</script>
Output:
Current month : 8
Updated month : 9
Example 2
Let's examine an example that demonstrates how to modify the month of a specified date.
<script>
var month=new Date("August 15, 1947 20:22:10");
month.setUTCMonth(10);
document.writeln("Updated month : "+(month.getUTCMonth()+1));
</script>
Output:
Example 3
In this illustration, we will additionally indicate the specific day along with the month.
<script>
var month=new Date("August 15, 1947 20:22:10");
month.setUTCMonth(10,31);
document.writeln("Updated month : "+(month.getUTCMonth()+1));
</script>
Output:
Updated month : 12