Date.prototype.getUTCHours()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

getUTCHours() 方法用於 Date 例項,根據通用時間(UTC)返回日期的小時部分。

試一試

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

瀏覽器相容性

另見