.setUTCMonth()
Published Jul 20, 2023
Contribute to Docs
The .setUTCMonth()
method sets the month of a Date
object according to universal time (UTC).
Syntax
dateObject.setUTCMonth(month)
dateObject.setUTCMonth(month, day)
The .setUTCMonth()
method is called on dateObject
with the integer input argument month
, and optionally day
.
month
(required): Expected values are in the range between0-11
, but other values are accepted:-1
will setdateObject
to the last month of the previous year12
will setdateObject
to the first month of the following year
day
(optional): Expected values are in the range between1-31
, but other values are accepted:- Any value smaller than
1
will roll over to the previous month (0
will setdateObject
to the last day of the previous month) - Any value greater than the number of days in a month will roll over to the following month
- Any value smaller than
Examples
This example sets the month and day and returns a variety of date formats to the console.
const dateObject = new Date('2020-05-10T00:00:00');dateObject.setUTCMonth(2, 20); // Month, Day (optional)console.log(dateObject);console.log(dateObject.toUTCString());console.log('Month: ' + dateObject.getUTCMonth(),'\nDay: ' + dateObject.getUTCDay());
The code above results in the following output:
2020-03-20T00:00:00.000ZFri, 20 Mar 2020 00:00:00 GMTMonth: 2Day: 5
Note: The first month of the year is
0
, the first day of the month is1
.
This example showcases the rollover of dates with values outside of the expected range:
const dateObject = new Date(Date.UTC(2000, 1, 2, 3, 4, 5)); // Year, Month, Day, Hour, Minute, Second, MilliseconddateObject.setUTCMonth(0, 1); // 1st month of year, 1st day of monthconsole.log(dateObject, '=', dateObject.toUTCString());dateObject.setUTCMonth(-1); // Month out of range, rolls over to previous yearconsole.log(dateObject, '=', dateObject.toUTCString());dateObject.setUTCMonth(12, 32); // Month and day out of range, rolls over to following year and monthconsole.log(dateObject, '=', dateObject.toUTCString());
The code above results in the following output:
2000-01-01T03:04:05.000Z = Sat, 01 Jan 2000 03:04:05 GMT1999-12-01T03:04:05.000Z = Wed, 01 Dec 1999 03:04:05 GMT2000-02-01T03:04:05.000Z = Tue, 01 Feb 2000 03:04:05 GMT
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.