JavaScript Date setUTCMonth()

Posted by

Method

Introduction to the JavaScript Date setUTCMonth() Method

The JavaScript Date setUTCMonth() method is used to set the month of a given date object, according to universal time. It takes a single argument, an integer representing the month (0-11).

Syntax

The syntax of the setUTCMonth() method is as follows:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

Date.setUTCMonth(month[, date])

[/dm_code_snippet]

The month argument is required, and it is an integer value representing the month of the year (0-11), with 0 representing January and 11 representing December. The optional date argument is an integer value representing the day of the month (1-31). If the date argument is not provided, the day of the month is not changed.

Examples

Consider the following example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setUTCMonth(3);
console.log(date);

[/dm_code_snippet]

In this example, we create a new Date object and then set the month to April (3). The result is a new Date object with the month set to April.

Using setUTCMonth() with a Date Argument

The setUTCMonth() method can also be used with a Date argument. Consider the following example:

[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]

var date = new Date();
date.setUTCMonth(3, 15);
console.log(date);

[/dm_code_snippet]

In this example, we create a new Date object and then set the month to April (3) and the day of the month to 15. The result is a new Date object with the month set to April and the day of the month set to 15.

Conclusion

The JavaScript Date setUTCMonth() method is used to set the month of a given date object, according to universal time. It takes a single argument, an integer representing the month (0-11), and an optional date argument that specifies the day of the month (1-31). The setUTCMonth() method can be used to quickly set the month of a Date object.