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:
setUTCMonth(monthValue)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
monthValue | required | An 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
.
setUTCMonth(monthValue, dateValue)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
monthValue | required | An integer between 0 and 11, representing the months January through December. |
dateValue | optional | An 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,
- Create a new Date object with the current date and time.
- Use the setUTCMonth() method to set the month to March (month value 2) UTC.
- 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,
- Create a new Date object with the current date and time.
- Use the setUTCMonth() method to set the month to March (month value 2) and the day to 15 UTC.
- 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.