Javascript - Date getUTCHours() Method

The getUTCHours() method returns the hour ranged from 0 to 23 of date object, according to universal time.

Description

The getUTCHours() method returns the hour ranged from 0 to 23 of date object, according to universal time.

UTC time is the same as GMT time.

Syntax

Date.getUTCHours()

Parameters

None

Return

A Number, from 0 to 23, representing the hour

Example

Return the hour, according to universal time:

Demo

//display the hour, according to UTC.

var d = new Date();
var n = d.getUTCHours();
console.log(n);//from w  w  w . j ava2  s . c o  m

//Return the UTC hour from a specific date and time:

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

Result

Using getUTCHours(), getUTCMinutes(), and getUTCSeconds() to display the universal time:

Demo

//display the UTC time.
function addZero(i) {
    if (i < 10) {
        i = "0" + i;
    }//from  w w w  .  j av a2s .  co m
    return i;
}

var d = new Date();
var h = addZero(d.getUTCHours());
var m = addZero(d.getUTCMinutes());
var s = addZero(d.getUTCSeconds());
console.log( h + ":" + m + ":" + s);

Result