JavaScript Date getUTCHours()
Syntax & Examples


Date.getUTCHours() method

The getUTCHours() method of the Date object in JavaScript returns the hours (0 – 23) in the specified date according to universal time.


Syntax of Date.getUTCHours()

The syntax of Date.getUTCHours() method is:

getUTCHours()

This getUTCHours() method of Date returns the hours (0 – 23) in the specified date according to universal time.

Return Type

Date.getUTCHours() returns value of type Number.



✐ Examples

1 Using getUTCHours() to get the UTC hours of a specific date

In JavaScript, we can use the getUTCHours() method to get the hours from a Date object according to universal time.

For example,

  1. We create a new Date object date representing January 15, 2021, at 15:30:00 UTC.
  2. We use the getUTCHours() method to get the hours from date.
  3. The hours are stored in the variable utcHours.
  4. We log utcHours to the console using console.log() method.

JavaScript Program

const date = new Date('2021-01-15T15:30:00Z');
const utcHours = date.getUTCHours();
console.log(utcHours);

Output

15

2 Using getUTCHours() with the current date

In JavaScript, we can use the getUTCHours() method to get the current hours from the current date according to universal time.

For example,

  1. We create a new Date object now representing the current date and time.
  2. We use the getUTCHours() method to get the current hours from now.
  3. The current hours are stored in the variable currentUTCHours.
  4. We log currentUTCHours to the console using console.log() method.

JavaScript Program

const now = new Date();
const currentUTCHours = now.getUTCHours();
console.log(currentUTCHours);

Output

13

3 Using getUTCHours() with a specific date in the past

In JavaScript, we can use the getUTCHours() method to get the hours from a specific date in the past according to universal time.

For example,

  1. We create a new Date object pastDate representing July 4, 1776, at 12:00:00 UTC.
  2. We use the getUTCHours() method to get the hours from pastDate.
  3. The hours are stored in the variable pastUTCHours.
  4. We log pastUTCHours to the console using console.log() method.

JavaScript Program

const pastDate = new Date('1776-07-04T12:00:00Z');
const pastUTCHours = pastDate.getUTCHours();
console.log(pastUTCHours);

Output

12

Summary

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