JavaScript Date setUTCHours()
Syntax & Examples


Date.setUTCHours() method

The setUTCHours() method sets the hour for a specified date according to universal time.


Syntax of Date.setUTCHours()

There are 4 variations for the syntax of Date.setUTCHours() method. They are:

1.
setUTCHours(hoursValue)

Parameters

ParameterOptional/RequiredDescription
hoursValuerequiredAn integer between 0 and 23, representing the hour.

This method sets the hour for the specified date according to universal time.

Returns value of type Number.

2.
setUTCHours(hoursValue, minutesValue)

Parameters

ParameterOptional/RequiredDescription
hoursValuerequiredAn integer between 0 and 23, representing the hour.
minutesValueoptionalAn integer between 0 and 59, representing the minutes.

This method sets the hour and minutes for the specified date according to universal time.

Returns value of type Number.

3.
setUTCHours(hoursValue, minutesValue, secondsValue)

Parameters

ParameterOptional/RequiredDescription
hoursValuerequiredAn integer between 0 and 23, representing the hour.
minutesValueoptionalAn integer between 0 and 59, representing the minutes.
secondsValueoptionalAn integer between 0 and 59, representing the seconds.

This method sets the hour, minutes, and seconds for the specified date according to universal time.

Returns value of type Number.

4.
setUTCHours(hoursValue, minutesValue, secondsValue, msValue)

Parameters

ParameterOptional/RequiredDescription
hoursValuerequiredAn integer between 0 and 23, representing the hour.
minutesValueoptionalAn integer between 0 and 59, representing the minutes.
secondsValueoptionalAn integer between 0 and 59, representing the seconds.
msValueoptionalAn integer between 0 and 999, representing the milliseconds.

This method sets the hour, minutes, seconds, and milliseconds for the specified date according to universal time.

Returns value of type Number.



✐ Examples

1 Using setUTCHours() method with one argument

In JavaScript, we can use the setUTCHours() method to set the hour of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCHours() method to set the hour to 10 UTC.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCHours(10);
console.log(date);

Output

Expected output: The date with the hour set to 10 UTC.

2 Using setUTCHours() method with two arguments

In JavaScript, we can use the setUTCHours() method to set the hour and minutes of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCHours() method to set the hour to 10 UTC and the minutes to 30.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCHours(10, 30);
console.log(date);

Output

Expected output: The date with the hour set to 10 UTC and the minutes set to 30.

3 Using setUTCHours() method with four arguments

In JavaScript, we can use the setUTCHours() method to set the hour, minutes, seconds, and milliseconds of a date according to universal time.

For example,

  1. Create a new Date object with the current date and time.
  2. Use the setUTCHours() method to set the hour to 10 UTC, the minutes to 30, the seconds to 45, and the milliseconds to 500.
  3. Log the updated date to the console using console.log().

JavaScript Program

const date = new Date();
date.setUTCHours(10, 30, 45, 500);
console.log(date);

Output

Expected output: The date with the hour set to 10 UTC, the minutes set to 30, the seconds set to 45, and the milliseconds set to 500.

Summary

In this JavaScript tutorial, we learned about setUTCHours() method of Date: the syntax and few working examples with output and detailed explanation for each example.