Javascript - Date setUTCMonth() Method

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

Description

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

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

You can use this method to set the day of the month.

UTC time is the same as GMT time.

Syntax

Date.setUTCMonth(month,day)

Parameter Values

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

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

  • -1 will result in the last month of the previous year
  • 12 will result in the first month of the next year
  • 13 will result in the second month of the next year

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

  • 0 will result in the last hour of the previous month
  • -1 will result in the hour before the last hour of the previous month
  • If the month has 31 days: 32 will result in the first day of the next month
  • If the month has 30 days: 32 will result in the second day of the next month

Return

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

Example

Set the month to 4 (May):

Demo

//display the date after changing the month.
var d = new Date();
d.setUTCMonth(4);/*from   w  ww  . j  a va2  s  .  co m*/
console.log(d);

//Set the month to 4 (May) and the day to the 20th:
//display the date after changing the month and day.
var d = new Date();
d.setUTCMonth(4, 20);
console.log(d);

//Set the date to be the last day of last month:
//display the date after setting the date to be the last day of last month.
var d = new Date();
d.setUTCMonth(d.getUTCMonth(), 0);
console.log(d);

Result