Javascript Reference - JavaScript setUTCMonth() Method








setUTCMonth(month)
Sets the month of the UTC date, which is any number 0 or greater. Numbers greater than 11 add years.

January is 0, February is 1, and so on.

This method can also be used to set the day of the month.

UTC time is the same as GMT time.

Browser Support

setUTCMonth() Yes Yes Yes Yes Yes

Syntax

dateObject.setUTCMonth(month, day);




Parameter Values

Parameter Description
month Required. An integer representing the month
Expected values are 0-11, but other values are allowed:
-1 will be the last month of the previous year
12 will be the first month of the next year
day Optional.

An integer representing the day of month
Expected values are 1-31, but other values are allowed:
0 will be the last hour of the previous month
-1 will be the hour before the last hour of the previous month

If the month has 31 days, 32 will be the first day of the next month





Return Value

return a number representing the number of milliseconds between the date object and midnight January 1 1970.

Example


var myDate = new Date();
      
myDate.setUTCMonth(01);
console.log(myDate.toString());

The code above generates the following result.

Example 2

The following code sets May 21st.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--   ww w.  j a v  a  2s .c  o m-->
    var d = new Date();
    d.setUTCMonth(4, 21);
    document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 3

The following code sets the date to be the last day of last month.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--  w w w  . j  a v  a2s .  co m-->
    var d = new Date();
    d.setUTCMonth(d.getUTCMonth(), 0);
    document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>

The code above is rendered as follows: