Date.prototype.getUTCMilliseconds()

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.

getUTCMilliseconds() 方法根據世界時回傳指定日期的毫秒數。

嘗試一下

const exampleDate = new Date("2018-01-02T03:04:05.678Z"); // 2 January 2018, 03:04:05.678 (UTC)

console.log(exampleDate.getUTCMilliseconds());
// Expected output: 678

語法

js
getUTCMilliseconds()

返回值

Date 物件為有效日期,則根據 UTC 時間回傳一個表示毫秒數、介於 0 至 999 之間的整數;若為無效日期,則回傳 Number.NaN()

別與 Unix 時間搞混了。應使用 Date.prototype.getTime() 方法取得自 1970/01/01 起經過的毫秒數。

範例

使用 getUTCMilliseconds()

下列範例指派當前時間的毫秒數至變數 milliseconds

js
const today = new Date();
const milliseconds = today.getUTCMilliseconds();

規範

Specification
ECMAScript® 2025 Language Specification
# sec-date.prototype.getutcmilliseconds

瀏覽器相容性

BCD tables only load in the browser

參見