大网数据广告活动
Web 标准与前端开发

Intl.DurationFormat.prototype.formatToParts()

· 52CE 我爱测

The **`formatToParts()`** method of Intl.DurationFormat instances returns an array of objects representing each part of the formatted string that would be returned by Intl/DurationFormat/format. It is useful for building custom strings from the locale-specific tokens.

The formatToParts() method of Intl.DurationFormat instances returns an array of objects representing each part of the formatted string that would be returned by Intl/DurationFormat/format. It is useful for building custom strings from the locale-specific tokens.

Syntax

formatToParts(duration)

Parameters

  • duration
    • : The duration object to be formatted. It should include some or all of the following properties: years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds. Each property's value should be an integer, and their signs should be consistent. This can be a Temporal.Duration object; see the Temporal.Duration documentation for more information about these properties.

Return value

An Array of objects containing the formatted duration in parts. Each object has two or three properties, type, value, and optionally unit, each containing a string. The string concatenation of value, in the order provided, will result in the same string as Intl/DurationFormat/format. The parts can be thought of as directly obtained from calling Intl/NumberFormat/formatToParts with the numeric value and their respective units. All tokens that are produced by the NumberFormat have an additional unit property, which is the singular form of the input unit; this is for programmatic usage and is not localized. The localized unit is output as a separate unit token as part of the NumberFormat result. The parts from each duration unit are concatenated together in the same fashion as calling Intl/ListFormat/formatToParts with { type: "unit" }, so additional literal tokens are inserted.

Examples

The formatToParts method enables locale-aware formatting of strings produced by DurationFormat formatters by providing you the string in parts:

const duration = {
  hours: 7,
  minutes: 8,
  seconds: 9,
  milliseconds: 123,
  microseconds: 456,
  nanoseconds: 789,
};

new Intl.DurationFormat("en", { style: "long" }).formatToParts(duration);

// Returned value:
[
  { type: "integer", value: "7", unit: "hour" },
  { type: "literal", value: " ", unit: "hour" },
  { type: "unit", value: "hours", unit: "hour" },
  { type: "literal", value: ", " },
  { type: "integer", value: "8", unit: "minute" },
  { type: "literal", value: " ", unit: "minute" },
  { type: "unit", value: "minutes", unit: "minute" },
  { type: "literal", value: ", " },
  { type: "integer", value: "9", unit: "second" },
  { type: "literal", value: " ", unit: "second" },
  { type: "unit", value: "seconds", unit: "second" },
  { type: "literal", value: ", " },
  { type: "integer", value: "123", unit: "millisecond" },
  { type: "literal", value: " ", unit: "millisecond" },
  { type: "unit", value: "milliseconds", unit: "millisecond" },
  { type: "literal", value: ", " },
  { type: "integer", value: "456", unit: "microsecond" },
  { type: "literal", value: " ", unit: "microsecond" },
  { type: "unit", value: "microseconds", unit: "microsecond" },
  { type: "literal", value: ", " },
  { type: "integer", value: "789", unit: "nanosecond" },
  { type: "literal", value: " ", unit: "nanosecond" },
  { type: "unit", value: "nanoseconds", unit: "nanosecond" },
];

Specifications

Browser compatibility

See also

  • Intl.DurationFormat
  • Intl/DurationFormat/format
  • Temporal.Duration

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。