Date.prototype.toLocaleTimeString()
toLocaleTimeString() 方法是 Date 例項的一個方法,它返回一個表示此日期的時間部分的、符合語言習慣的本地化字串(使用本地時區)。在支援 Intl.DateTimeFormat API 的實現中,此方法會委託給 Intl.DateTimeFormat。
每次呼叫 toLocaleTimeString 時,都需要在一個龐大的本地化字串資料庫中進行搜尋,這可能會導致效率低下。當方法使用相同的引數被呼叫多次時,最好建立一個 Intl.DateTimeFormat 物件並使用其 format() 方法,因為 DateTimeFormat 物件會記住傳遞給它的引數,並可能決定快取資料庫的一部分,以便將來的 format 呼叫可以在更受限的上下文中搜索本地化字串。
試一試
// Depending on timezone, your results will vary
const event = new Date("August 19, 1975 23:15:30 GMT+00:00");
console.log(event.toLocaleTimeString("en-US"));
// Expected output: "1:15:30 AM"
console.log(event.toLocaleTimeString("it-IT"));
// Expected output: "01:15:30"
console.log(event.toLocaleTimeString("ar-EG"));
// Expected output: "١٢:١٥:٣٠ ص"
語法
toLocaleTimeString()
toLocaleTimeString(locales)
toLocaleTimeString(locales, options)
引數
locales 和 options 引數自定義函式行為,並允許應用程式指定應使用的語言的格式約定。
在支援 Intl.DateTimeFormat API 的實現中,這些引數與 Intl.DateTimeFormat() 建構函式的引數完全對應。不支援 Intl.DateTimeFormat 的實現則被要求忽略這兩個引數,使得所使用的區域設定和返回字串的格式完全取決於具體實現。
locales可選-
一個帶有 BCP 47 語言標籤 的字串,或此類字串的陣列。對應於
Intl.DateTimeFormat()建構函式的locales引數。在不支援
Intl.DateTimeFormat的實現中,將忽略此引數,通常會使用主機的區域設定。 options可選-
一個調整輸出格式的物件。對應於
Intl.DateTimeFormat()建構函式的options引數。如果dayPeriod、hour、minute、second和fractionalSecondDigits都未定義,則hour、minute、second將被設定為"numeric"。在不支援
Intl.DateTimeFormat的實現中,將忽略此引數。
有關這些引數及其使用方法的詳細資訊,請參閱 Intl.DateTimeFormat() 建構函式。
返回值
一個根據語言特定約定表示給定日期時間部分的字串。
在支援 Intl.DateTimeFormat 的實現中,這等同於 new Intl.DateTimeFormat(locales, options).format(date),其中 options 已按上述方式規範化。
注意: 大多數情況下,toLocaleTimeString() 返回的格式是保持一致的。然而,輸出在不同實現之間可能有所不同,即使是在同一 locale 下——輸出的變化是設計使然,並符合規範的允許。它也可能不是你期望的。例如,字串可能使用不間斷空格,或者被雙向控制字元包圍。你不應該將 toLocaleTimeString() 的結果與硬編碼的常量進行比較。
示例
使用 toLocaleTimeString()
不指定 locale 的此方法的簡單用法以預設區域設定和預設選項返回格式化字串。
const date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
// toLocaleTimeString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleTimeString());
// "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
檢查對 locales 和 options 引數的支援
locales 和 options 引數可能並非在所有實現中都得到支援,因為國際化 API 的支援是可選的,並且某些系統可能沒有必要的資料。對於沒有國際化支援的實現,toLocaleTimeString() 總是使用系統的 locale,這可能不是你想要的。由於任何支援 locales 和 options 引數的實現都必須支援 Intl API,你可以檢查後者的存在性來判斷支援情況。
function toLocaleTimeStringSupportsLocales() {
return (
typeof Intl === "object" &&
!!Intl &&
typeof Intl.DateTimeFormat === "function"
);
}
使用語言環境
本示例顯示了一些本地化時間格式的變體。為了獲得應用程式使用者介面中使用的語言的格式,請確保使用 locales 引數指定該語言(以及可能的備用語言)。
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses 12-hour time with AM/PM
console.log(date.toLocaleTimeString("en-US"));
// "7:00:00 PM"
// British English uses 24-hour time without AM/PM
console.log(date.toLocaleTimeString("en-GB"));
// "03:00:00"
// Korean uses 12-hour time with AM/PM
console.log(date.toLocaleTimeString("ko-KR"));
// "오후 12:00:00"
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleTimeString("ar-EG"));
// "٧:٠٠:٠٠ م"
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(date.toLocaleTimeString(["ban", "id"]));
// "11.00.00"
使用選項
可以使用 options 引數來自定義 toLocaleTimeString() 提供的結果。
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// An application may want to use UTC and make that visible
const options = { timeZone: "UTC", timeZoneName: "short" };
console.log(date.toLocaleTimeString("en-US", options));
// "3:00:00 AM GMT"
// Sometimes even the US needs 24-hour time
console.log(date.toLocaleTimeString("en-US", { hour12: false }));
// "19:00:00"
// Show only hours and minutes, use options with the default locale - use an empty array
console.log(
date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }),
);
// "20:01"
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-date.prototype.tolocaletimestring |
| ECMAScript® 2026 國際化 API 規範 # sup-date.prototype.tolocaletimestring |
瀏覽器相容性
載入中…