Method
JavaScript Date getUTCHours() Method
The JavaScript Date.getUTCHours() method returns the hour for the specified date according to universal time (UTC). The value returned by getUTCHours() is an integer between 0 and 23.
Syntax
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
dateObj.getUTCHours()
[/dm_code_snippet]
Parameters
The getUTCHours() method does not accept any parameters.
Return Value
The getUTCHours() method returns an integer between 0 and 23, representing the hour for the specified date according to universal time (UTC).
Browser Support
The getUTCHours() method is supported in all major browsers, including Internet Explorer 9+, Firefox, Chrome, Edge, and Safari.
Examples
Let’s look at some examples of the getUTCHours() method.
Example: Get the Hours from a Date Object
In the following example, we create a Date object, and then use the getUTCHours() method to get the hour for the specified date according to universal time (UTC):
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
var d = new Date();
var hours = d.getUTCHours();
console.log(hours);
[/dm_code_snippet]
This will output the following:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
8
[/dm_code_snippet]
This is the hour for the specified date according to universal time (UTC).
Example: Get the Hours from a UTC Date String
In the following example, we create a Date object with a UTC date string, and then use the getUTCHours() method to get the hour for the specified date according to universal time (UTC):
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
var d = new Date("Tue Jan 15 2019 13:30:00 GMT+0530");
var hours = d.getUTCHours();
console.log(hours);
[/dm_code_snippet]
This will output the following:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
8
[/dm_code_snippet]
This is the hour for the specified date according to universal time (UTC).
Conclusion
The JavaScript Date.getUTCHours() method is a useful tool for getting the hour for the specified date according to universal time (UTC). It accepts no parameters and returns an integer between 0 and 23, representing the hour for the specified date according to universal time (UTC).