Intl.DurationFormat

基准线 2025
新推出

自 ⁨2025 年 3 月⁩ 起,此功能可在最新的设备和浏览器版本上使用。此功能可能在旧设备或浏览器上无法正常工作。

Intl.DurationFormat 对象支持实现语言敏感的持续时间格式化。

构造函数

Intl.DurationFormat()

创建一个新的 Intl.DurationFormat 对象。

静态方法

Intl.DurationFormat.supportedLocalesOf()

返回一个数组,其中包含提供的区域设置中受支持的那些区域设置,而无需回退到运行时默认区域设置。

实例属性

这些属性定义在 Intl.DurationFormat.prototype 上,并由所有 Intl.DurationFormat 实例共享。

Intl.DurationFormat.prototype.constructor

创建实例对象的构造函数。对于 Intl.DurationFormat 实例,初始值是 Intl.DurationFormat 构造函数。

Intl.DurationFormat.prototype[Symbol.toStringTag]

[Symbol.toStringTag] 属性的初始值是字符串 "Intl.DurationFormat"。此属性用于 Object.prototype.toString()

实例方法

Intl.DurationFormat.prototype.format()

获取器函数,用于根据此 DurationFormat 对象的区域设置和格式化选项来格式化持续时间。

Intl.DurationFormat.prototype.formatToParts()

返回一个对象数组,其中包含格式化持续时间的分成部分。

Intl.DurationFormat.prototype.resolvedOptions()

返回一个新对象,其属性反映在对象初始化过程中计算出的语言环境和格式化选项。

示例

使用 Intl.DurationFormat

下面的示例展示了如何使用 Intl.DurationFormat 对象,根据各种区域设置和样式格式化持续时间对象。

js
const duration = {
  hours: 1,
  minutes: 46,
  seconds: 40,
};

// With style set to "long" and locale "fr-FR"
new Intl.DurationFormat("fr-FR", { style: "long" }).format(duration);
// "1 heure, 46 minutes et 40 secondes"

// With style set to "short" and locale "en"
new Intl.DurationFormat("en", { style: "short" }).format(duration);
// "1 hr, 46 min and 40 sec"

// With style set to "narrow" and locale "pt"
new Intl.DurationFormat("pt", { style: "narrow" }).format(duration);
// "1 h 46 min 40 s"

规范

规范
Intl.DurationFormat
# durationformat-objects

浏览器兼容性

另见