Date.prototype.getUTCHours()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Date
實例的 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());
// 預期輸出:12
console.log(date2.getUTCHours());
// 預期輸出:10
語法
js
getUTCHours()
參數
無。
回傳值
一個介於 0 到 23 之間的整數,表示給定日期根據世界標準時間的小時。如果日期為無效的,則回傳 NaN
。
範例
使用 getUTCHours()
以下範例將目前時間的小時部分指派給 hours
變數。
js
const today = new Date();
const hours = today.getUTCHours();
規範
Specification |
---|
ECMAScript® 2026 Language Specification # sec-date.prototype.getutchours |