试一试
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
语法
js
getUTCHours()
参数
无。
返回值
返回一个介于 0 和 23 之间的整数,表示给定日期根据通用时间(UTC)的小时。如果日期 无效,则返回 NaN。
示例
使用 getUTCHours()
以下示例将当前时间的时部分赋值给变量 hours。
js
const today = new Date();
const hours = today.getUTCHours();
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-date.prototype.getutchours |
浏览器兼容性
加载中…