Home >>JavaScript Date Object Methods >JavaScript setMonth() Method

JavaScript setMonth() Method

JavaScript setMonth() Method

JavaScript setMonth() method is an inbuilt method in JavaScript which is used to set the month of a date object according to local time. Value of month is from 0 to 11. 0 indicates January, 1 indicates February and so on till 11 for December.

Syntax:
Date.setMonth(month, day)

Parameter Values

Parameter Description
month It is Required. An integer (0-11) representing the month.
day It is Optional. An integer from 1-31 representing the day of the month.

Browser Support

Method Chrome Edge Firefox Safari Opera
setMonth() Yes Yes Yes Yes Yes
Here is an Example of setMonth() Method:
<html>
<body>
<button onclick="mySetMonth()">click me</button>
<p id="SM"></p>
<script>
function mySetMonth() 
{
  var c = new Date();
  c.setMonth(8);
  document.getElementById("SM").innerHTML = c;
}
</script>
</body>
</html>
Output:

Example 2:
<html>
<body>
<script> 
var dateobj = new Date(); 
dateobj.setMonth(1); 
var x = dateobj.getMonth(); 
var y = dateobj.getDate(); 
var z = dateobj.getFullYear(); 
document.write(x + "<br>"); 
document.write(y + "<br>"); 
document.write(z); 
</script> 
</body>
</html>
Output:

No Sidebar ads