Get to know MDN better
此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.
Date.UTC() 靜態方法接受與 Date 建構子相似的日期和時間元件參數,但將其視為 UTC。它會回傳自 1970 年 1 月 1 日 00:00:00 UTC 以來的毫秒數。
代表年份的整數值。0 到 99 之間的值會對應到 1900 到 1999 年。所有其他值都是實際年份。請參見範例。
monthIndex 選擇性代表月份的整數值,從 0(一月)到 11(十二月)。預設為 0。
day 選擇性代表月份中日期的整數值。預設為 1。
hours 選擇性代表一天中小時的整數值,介於 0 到 23 之間。預設為 0。
minutes 選擇性代表時間中分鐘部分的整數值。預設為 0。
seconds 選擇性代表時間中秒數部分的整數值。預設為 0。
milliseconds 選擇性代表時間中毫秒部分的整數值。預設為 0。
一個數字,代表給定日期的時間戳。如果日期無效,則回傳 NaN。
介於 0 和 99 之間的年份會被轉換為 20 世紀的年份(1900 + year)。例如,95 會被轉換為 1995 年。
UTC() 方法與 Date() 建構子有三點不同:
如果參數超出預期範圍,UTC() 方法會更新其他參數以容納該值。例如,如果 monthIndex 使用 15,年份將會增加 1(year + 1),而月份將使用 3。
因為 UTC() 是 Date 的一個靜態方法,所以你總是使用 Date.UTC(),而不是在你建立的 Date 物件上作為方法使用。
以下陳述式會建立一個 Date 物件,其引數會被視為 UTC 而非本地時間:
當傳入單一引數時,Date.UTC() 過去的行為不一致,因為實作僅與 Date() 建構子保持行為一致,而後者不會將單一引數解釋為年份數字。現在要求實作將省略的 monthIndex 視為 0,而不是將其強制轉換為 NaN。
| ECMAScript® 2027 Language Specification # sec-date.utc |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025年8月20日 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.