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,
- We create a new Date object
daterepresenting January 15, 2021, at 15:30:00 UTC. - We use the
getUTCHours()method to get the hours fromdate. - The hours are stored in the variable
utcHours. - We log
utcHoursto the console usingconsole.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,
- We create a new Date object
nowrepresenting the current date and time. - We use the
getUTCHours()method to get the current hours fromnow. - The current hours are stored in the variable
currentUTCHours. - We log
currentUTCHoursto the console usingconsole.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,
- We create a new Date object
pastDaterepresenting July 4, 1776, at 12:00:00 UTC. - We use the
getUTCHours()method to get the hours frompastDate. - The hours are stored in the variable
pastUTCHours. - We log
pastUTCHoursto the console usingconsole.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.