Intl.DurationFormat() 建構函式

基準線 2025
新推出

自 ⁨2025 年 3 月⁩ 起,此功能可在最新的裝置和瀏覽器版本上使用。此功能可能在舊裝置或瀏覽器上無法正常工作。

Intl.DurationFormat() 建構函式用於建立 Intl.DurationFormat 物件。

語法

js
new Intl.DurationFormat()
new Intl.DurationFormat(locales)
new Intl.DurationFormat(locales, options)

注意: Intl.DurationFormat() 只能使用 new 來構造。嘗試不帶 new 呼叫它會丟擲 TypeError

引數

locales 可選

包含 BCP 47 語言標籤的字串或 Intl.Locale 例項,或此類區域設定識別符號的陣列。當傳入 undefined 或未指定支援的區域設定識別符號時,將使用執行時的預設區域設定。有關 locales 引數的一般形式和解釋,請參閱 Intl 主頁上的引數描述

允許使用以下 Unicode 擴充套件鍵

nu

請參閱 numberingSystem

此鍵也可以透過 options(如下所列)設定。當兩者都設定時,options 屬性優先。

options 可選

一個包含以下屬性的物件,按照它們被檢索的順序排列(所有屬性都是可選的)

localeMatcher

要使用的區域設定匹配演算法。可能的值是 "lookup""best fit";預設值為 "best fit"。有關此選項的資訊,請參閱 區域設定標識和協商

numberingSystem

用於數字格式化的編號系統,例如 "arab""hans""mathsans" 等。有關支援的編號系統型別的列表,請參閱 Intl.supportedValuesOf();預設值取決於區域設定。此選項也可以透過 nu Unicode 擴充套件鍵設定;如果兩者都提供,則此 options 屬性優先。

style

格式化持續時間時使用的樣式。此值用作所有其他單位選項的預設值,並且在連線持續時間單位列表時也對應於 Intl.ListFormat()style 選項。可能的值有:

"long"

例如,1 小時 50 分鐘

"short"(預設)

例如,1 小時,50 分鐘

"narrow"

例如,1h 50m

"digital"

例如,1:50:00

years

格式化年數時使用的樣式。可能的值為 "long""short""narrow";預設值為 options.style(如果不是 "digital"),否則為 "short"

yearsDisplay

是否始終顯示年數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 years,則預設為 "auto",否則預設為 "always"

months

格式化月數時使用的樣式。可能的值為 "long""short""narrow";預設值為 options.style(如果不是 "digital"),否則為 "short"

monthsDisplay

是否始終顯示月數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 months,則預設為 "auto",否則預設為 "always"

weeks

格式化週數時使用的樣式。可能的值為 "long""short""narrow";預設值為 options.style(如果不是 "digital"),否則為 "short"

weeksDisplay

是否始終顯示週數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 weeks,則預設為 "auto",否則預設為 "always"

days

格式化天數時使用的樣式。可能的值為 "long""short""narrow";預設值為 options.style(如果不是 "digital"),否則為 "short"

daysDisplay

是否始終顯示天數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 days,則預設為 "auto",否則預設為 "always"

hours

格式化小時數時使用的樣式。可能的值為 "long""short""narrow""numeric""2-digit";預設值為 options.style(如果不是 "digital"),否則為 "numeric"

hoursDisplay

是否始終顯示小時數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 hoursoptions.style 不是 "digital",則預設為 "auto",否則預設為 "always"

minutes

格式化分鐘數時使用的樣式。

  • 如果 hours"numeric""2-digit",可能的值為 "numeric""2-digit",並且 "numeric" 會被標準化為 "2-digit";預設值為 "numeric"
  • 否則,可能的值為 "long""short""narrow""numeric""2-digit";預設值為 options.style(如果不是 "digital"),否則為 "numeric"
minutesDisplay

是否始終顯示分鐘數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 minutesoptions.style 不是 "digital",則預設為 "auto",否則預設為 "always"

seconds

格式化秒數時使用的樣式。

  • 如果 minutes"numeric""2-digit",可能的值為 "numeric""2-digit",並且 "numeric" 會被標準化為 "2-digit";預設值為 "numeric"
  • 否則,可能的值為 "long""short""narrow""numeric""2-digit";預設值為 options.style(如果不是 "digital"),否則為 "numeric"
secondsDisplay

是否始終顯示秒數,或僅在非零時顯示。可能的值為 "always""auto";如果未指定 secondsoptions.style 不是 "digital",則預設為 "auto",否則預設為 "always"

milliseconds

格式化毫秒時使用的樣式。

  • 如果 seconds"numeric""2-digit",唯一可能的值是 "numeric";預設值為 "numeric"
  • 否則,可能的值為 "long""short""narrow""numeric";預設值為 options.style(如果不是 "digital"),否則為 "numeric"
millisecondsDisplay

是否始終顯示毫秒,或僅在非零時顯示。

  • 如果 seconds"numeric""2-digit",唯一可能的值是 "auto";只有當 milliseconds 未指定時,預設值為 "auto"
  • 否則,可能的值為 "always""auto";如果未指定 milliseconds,則預設為 "auto",否則預設為 "always"
microseconds

格式化微秒時使用的樣式。

  • 如果 milliseconds"numeric",唯一可能的值是 "numeric";預設值為 "numeric"
  • 否則,可能的值為 "long""short""narrow""numeric";預設值為 options.style(如果不是 "digital"),否則為 "numeric"
microsecondsDisplay

是否始終顯示微秒,或僅在非零時顯示。

  • 如果 milliseconds"numeric",唯一可能的值是 "auto";只有當 microseconds 未指定時,預設值為 "auto"
  • 否則,可能的值為 "always""auto";如果未指定 microseconds,則預設為 "auto",否則預設為 "always"
nanoseconds

格式化納秒時使用的樣式。

  • 如果 microseconds"numeric",唯一可能的值是 "numeric";預設值為 "numeric"
  • 否則,可能的值為 "long""short""narrow""numeric";預設值為 options.style(如果不是 "digital"),否則為 "numeric"
nanosecondsDisplay

是否始終顯示納秒,或僅在非零時顯示。

  • 如果 microseconds"numeric",唯一可能的值是 "auto";只有當 nanoseconds 未指定時,預設值為 "auto"
  • 否則,可能的值為 "always""auto";如果未指定 nanoseconds,則預設為 "auto",否則預設為 "always"
fractionalDigits

要在輸出中顯示的秒的小數位數。可能的值為 09;預設值為 undefined(包含必要的小數位數)。

異常

RangeError

如果 localesoptions 包含無效值,則丟擲此錯誤。

描述

對於每個時間段,後臺都會構造一個 Intl.NumberFormat 物件。它使用以下選項(有關詳細資訊,請參閱 Intl.NumberFormat()

  • numberingSystemoptions.numberingSystem 的值

millisecondsmicrosecondsnanoseconds 使用 "numeric" 樣式時,還將使用以下選項:

  • minimumFractionDigits:當 options.fractionalDigitsundefined 時為 0,否則為 options.fractionalDigits
  • maximumFractionDigits:當 options.fractionalDigitsundefined 時為 9,否則為 options.fractionalDigits
  • roundingMode"trunc"

當時間段使用 "2-digit" 樣式時,還將使用以下選項:

  • minimumIntegerDigits2

當時間段使用 "long""short""narrow" 樣式時,還將使用以下選項:

  • style:如果指定了 "long""short""narrow",則為 "unit",否則為 undefined
  • unit:當前格式化的單位("years""days""nanoseconds" 等)
  • unitDisplay:時間段樣式的("long""short""narrow")值

示例

使用 Intl.DurationFormat() 建構函式

js
const duration = {
  hours: 2,
  minutes: 20,
  seconds: 35,
};

console.log(new Intl.DurationFormat("pt", { style: "long" }).format(duration));
// "2 horas, 20 minutos e 35 segundos"

規範

規範
Intl.DurationFormat
# sec-intl-durationformat-constructor

瀏覽器相容性

另見