此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Date.prototype.getUTCHours()

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

getUTCHours() 方法以世界时为标准,返回一个指定的日期对象的小时数。

尝试一下

const date1 = new Date("December 31, 1975, 23:15:30 GMT+11:00");
const date2 = new Date("December 31, 1975, 23:15:30 GMT-11:00");

console.log(date1.getUTCHours());
// Expected output: 12

console.log(date2.getUTCHours());
// Expected output: 10

语法

dateObj.getUTCHours()

参数

无。

返回值

getUTCHours() 返回一个 0 到 23 的整数。

示例

示例:使用 getUTCHours() 方法

下例将当前时间的小时部分赋值给变量 hours

js
var today = new Date();
var hours = today.getUTCHours();

规范

规范
ECMAScript® 2027 Language Specification
# sec-date.prototype.getutchours

浏览器兼容性

参见