JavaScript Date setUTCHours() method

The setUTCHours method in JavaScript is employed to establish the hour for a given date according to Coordinated Universal Time (UTC).

Syntax

The syntax for the setUTCHours method is as follows:

Example

dateObj.setUTCHours(hoursValue[, minValue[, secValue[, msValue]]])

Parameter

hoursValue - This denotes an integer ranging from 0 to 23, indicating the hour of the day. In cases where the specified hour exceeds 23, the setUTCHours function adjusts the day value accordingly.

minValue - This parameter is optional. It signifies an integer ranging from 0 to 59, denoting the minutes. Should the specified minute value exceed 59, the setUTCHours function will adjust the hour value as necessary.

secValue - This parameter is not mandatory. It denotes an integer ranging from 0 to 59, which indicates the seconds. Should the supplied second value exceed 59, the setUTCHours method will adjust the minute value as necessary.

msValue - This parameter is not mandatory. It denotes an integer ranging from 0 to 999, indicating the number of milliseconds. In cases where the supplied millisecond value exceeds 999, the setUTCHours function will adjust the seconds value appropriately.

JavaScript Date setUTCHours method example

In this section, we will explore the setUTCHours method by examining a variety of examples.

Example 1

Let’s examine an example that demonstrates how to display both the current hour and the updated hour value.

Example

<script>

var hours=new Date();	

document.writeln("Current Hour : "+hours.getUTCHours()+"<br>");

hours.setUTCHours(10);

document.writeln("Updated Hour : "+hours.getUTCHours());

</script>

Output:

Output

Current Hour : 7

Updated Hour : 10

Example 2

Let’s consider an example that demonstrates how to modify the hour component of a specified time.

Example

<script>

var hours=new Date("August 15, 1947 20:22:10");	

hours.setUTCHours(10);

document.writeln("Updated Hour : "+hours.getUTCHours());

</script>

Output:

Output

Updated Hour : 10

Example 3

In this instance, we will define the minute value (exceeding 59) in conjunction with the hours.

Example

<script>

var hours=new Date("August 15, 1947 20:22:10");	

hours.setUTCHours(10,62);

document.writeln("Updated Hour : "+hours.getUTCHours());

</script>

Output:

Output

Updated Hour : 11

Input Required

This code uses input(). Please provide values below: