Date.prototype.toISOString()

Baseline 已廣泛支援

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

toISOString() 方法用於 Date 例項,返回一個字串,該字串表示此日期符合 日期時間字串格式。這是一種基於 ISO 8601簡化格式,長度始終為 24 或 27 個字元(分別是 YYYY-MM-DDTHH:mm:ss.sssZ±YYYYYY-MM-DDTHH:mm:ss.sssZ)。時區始終是 UTC,用字尾 Z 表示。

試一試

const event = new Date("05 October 2011 14:48 UTC");
console.log(event.toString());
// Expected output: "Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)"
// Note: your timezone may vary

console.log(event.toISOString());
// Expected output: "2011-10-05T14:48:00.000Z"

語法

js
toISOString()

引數

無。

返回值

根據通用時間,返回一個表示給定日期的日期時間字串格式的字串。它與 Date.parse() 需要識別的格式相同。

異常

RangeError

如果日期 無效,或者它對應的年份無法在日期字串格式中表示,則丟擲該錯誤。

示例

使用 toISOString()

js
const d = new Date(0);

console.log(d.toISOString()); // "1970-01-01T00:00:00.000Z"

規範

規範
ECMAScript® 2026 語言規範
# sec-date.prototype.toisostring

瀏覽器相容性

另見