JavaScript Date getUTCHours() Method

Definition and Usage

getUTCHours() The method returns the hour number (from 0 to 23) for a specified date and time based on UTC.

When calculating dates, the UTC methods assume the date object is the local time and date.

Tip:Coordinated Universal Time (UTC) is the standard time set for world time.

Note:Coordinated Universal Time (UTC) is the same as Greenwich Mean Time (GMT).

Example

Example 1

Return the hour number based on Coordinated Universal Time (UTC):

var d = new Date();
var n = d.getUTCHours();

Try It Yourself

Example 2

Return the UTC hour for a specific date and time:

var d = new Date("July 21, 1983 01:15:00");
var n = d.getUTCHours();

Try It Yourself

Example 3

Display Coordinated Universal Time (UTC) using getUTCHours(), getUTCMinutes(), and getUTCSeconds():

function addZero(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}
function myFunction() {
  var d = new Date();
  var x = document.getElementById("demo");
  var h = addZero(d.getUTCHours());
  var m = addZero(d.getUTCMinutes());
  var s = addZero(d.getUTCSeconds());
  x.innerHTML = h + ":" + m + ":" + s;
}

Try It Yourself

Syntax

Date.getUTCHours()

Parameters

No parameters.

Technical Details

Return Value: Numbers, from 0 to 23, represent hours.
JavaScript Version: ECMAScript 1

Browser Support

Method Chrome IE Firefox Safari Opera
getUTCHours() Support Support Support Support Support

Related Pages

Tutorial:JavaScript Date

Tutorial:JavaScript Date Format

Tutorial:JavaScript Object Constructor