JavaScript Date setUTCMonth()
Syntax & Examples


Date.setUTCMonth() method

The setUTCMonth() method sets the month for a specified date according to universal time.


Syntax of Date.setUTCMonth()

There are 2 variations for the syntax of Date.setUTCMonth() method. They are:

1.
setUTCMonth(monthValue)

Parameters

ParameterOptional/RequiredDescription
monthValuerequiredAn integer between 0 and 11, representing the months January through December.

This method sets the month for the specified date according to universal time.

Returns value of type Number.

2.
setUTCMonth(monthValue, dateValue)

Parameters

ParameterOptional/RequiredDescription
monthValuerequiredAn integer between 0 and 11, representing the months January through December.
dateValueoptionalAn integer between 1 and 31, representing the day of the month.

This method sets the month and day for the specified date according to universal time.

Returns value of type Number.



✐ Examples

1 Using setUTCMonth() method with one argument

In JavaScript, we can use the setUTCMonth() method to set the month of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCMonth() method to set the month to March (month value 2) UTC.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCMonth(2);
console.log(date);

Output

Expected output: The date with the month set to March UTC.

2 Using setUTCMonth() method with two arguments

In JavaScript, we can use the setUTCMonth() method to set the month and day of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCMonth() method to set the month to March (month value 2) and the day to 15 UTC.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCMonth(2, 15);
console.log(date);

Output

Expected output: The date with the month set to March and the day set to 15 UTC.

Summary

In this JavaScript tutorial, we learned about setUTCMonth() method of Date: the syntax and few working examples with output and detailed explanation for each example.