The JavaScript setUTCHours function sets hours, minutes, seconds, and milliseconds of a specified date according to Universal Coordinated Time (UTC).
The syntax of the setUTCHours function is:
Date.setUTCHours(Hours, Minutes, Seconds, Milliseconds)
Parameters: As you can see, the setUTCHours() method accepts four parameters, and the first one (hours) is required, and the remaining three are optional.
Return Value: It updates the given date by user given hours, minutes, seconds, and milliseconds. If we use it against an invalid date, it returns NaN.
JavaScript setUTCHours example
In the following example, we use the setUTCHours to set the current UTC hours to 05. As you can see from the output, the current UTC is 10, and the function sets it to 05:07:27 GMT.
const dt = new Date();
console.log(dt.toUTCString());
dt.setUTCHours(5);
console.log(dt.toUTCString());
Mon, 15 Jun 2026 10:07:27 GMT
Mon, 15 Jun 2026 05:07:27 GMT
Example 2: Here, we used the Date() constructor to parse the string date and then applied the SetUTCHours() method to set the UTC hours to 12.
const dt = new Date('November 14, 2023 10:35:12 GMT-4:00');
console.log(dt.toUTCString());
dt.setUTCHours(12);
console.log(dt.toUTCString());
Tue, 14 Nov 2023 14:35:12 GMT
Tue, 14 Nov 2023 12:35:12 GMT
Example 3: If we use the numbers outside the range, the JavaScript setUTCHours() method will update the days accordingly. For instance, if set hours to 40, it adds one day and 16 Hours to the current date and time.
const dt = new Date();
console.log(dt.toUTCString());
dt.setUTCHours(40);
console.log(dt.toUTCString());
Mon, 15 Jun 2026 12:44:32 GMT
Tue, 16 Jun 2026 16:44:32 GMT
Similarly, if we use minutes and seconds greater than 59 and milliseconds greater than 999, it does the same thing.
const dt = new Date();
console.log(dt.toUTCString());
dt.setUTCHours(2, 70, 80);
console.log(dt.toUTCString());
Mon, 15 Jun 2026 12:45:45 GMT
Mon, 15 Jun 2026 03:11:20 GMT
Difference between setHours and setUTCHours?
The setHours method sets the hours value based on the user’s local system time zone. On the other hand, the setUTCHours() sets the time based on UTC (Universal Coordinated Time) zone.
How to set UTC Hours, minutes, and seconds in JavaScript?
The JavaScript setUTCHours function contains Hours, Minutes, Seconds, and Milliseconds; among them, the last three are optional arguments. If we use them we can set UTC hours, minutes, seconds, and milliseconds of a datetime.
In this example, we set the current UTC date hours to 12, minutes to 22, seconds to 45, and milliseconds to 222.
const dt = new Date();
console.log(dt.toUTCString());
dt.setUTCHours(12, 22, 45, 222);
console.log(dt.toUTCString());
console.log(dt.getUTCHours());
console.log(dt.getUTCMinutes());
console.log(dt.getUTCSeconds());
console.log(dt.getUTCMilliseconds());
Mon, 15 Jun 2026 10:13:09 GMT
Mon, 15 Jun 2026 12:22:45 GMT
12
22
45
222
TIP: Please use getUTCHours(), getUTCMinutes(), getUTCSeconds(), and getUTCMilliseconds() methods to extract UTC hours, minutes, seconds, and milliseconds values.