Date setUTCMonth() Method - Javascript Date

Javascript examples for Date:setUTCMonth

Description

The setUTCMonth() method sets the month from 0 to 11, according to universal time.

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

UTC time is the same as GMT time.

Syntax

date.setUTCMonth(month, day);

Parameter Values

Parameter Description
month Required. An integer representing the month
day Optional. An integer representing the day of month

month expected values are 0-11, but other values are allowed:

  • -1 sets the last month of the previous year
  • 12 sets the first month of the next year
  • 13 sets the second month of the next year

day expected values are 1-31, but other values are allowed:

  • 0 sets the last day of the previous month
  • -1 sets the day before the last day of the previous month
  • If the month has 31 days, 32 sets the first day of the next month
  • If the month has 30 days, 32 sets the second day of the next month

Return Value:

A Number, representing the number of milliseconds between the date object and midnight January 1 1970

display the date after setting the date to be the last day of last month.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//from   w  ww .j a v  a  2 s.c  om
    var d = new Date();
    d.setUTCMonth(d.getUTCMonth(), 0);
    document.getElementById("demo").innerHTML = d;
}
</script>

</body>
</html>

Related Tutorials