Intl.DurationFormat.prototype.formatToParts()
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Intl.DurationFormat 实例的 formatToParts() 方法允许对 Intl.DurationFormat 格式化程序生成的字符串进行区域设置感知格式化。
¥The formatToParts() method of Intl.DurationFormat instances allows locale-aware formatting of strings produced by Intl.DurationFormat formatters.
语法
参数
¥Parameters
durationOptional-
要格式化的持续时间对象。它应包括以下部分或全部属性:
"months","weeks","days","hours","minutes","seconds","milliseconds","microseconds","nanoseconds".
返回值
描述
¥Description
formatToParts() 方法对于持续时间对象的自定义格式很有用。它返回 Array 个对象,其中包含特定于区域设置的标记,可以从中构建自定义字符串,同时保留特定于区域设置的部分。formatToParts() 方法返回的结构如下所示:
¥The formatToParts() method is useful for custom formatting of duration objects. It returns an Array of objects containing the locale-specific tokens from which it possible to build custom strings while preserving the locale-specific parts. The structure the formatToParts() method returns, looks like this:
[
{ type: "integer", value: "7", unit: "hour" },
{ type: "literal", value: " ", unit: "hour" },
{ type: "unit", value: "hr", unit: "hour" },
{ type: "literal", value: ", " },
{ type: "integer", value: "8", unit: "minute" },
{ type: "literal", value: " ", unit: "minute" },
{ type: "unit", value: "min", unit: "minute" },
];
示例
¥Examples
formatToParts 方法通过为你提供部分字符串来启用 DurationFormat 格式化程序生成的字符串的区域设置感知格式:
¥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: " and " },
{ type: "integer", value: "789", unit: "nanosecond" },
{ type: "literal", value: " ", unit: "nanosecond" },
{ type: "unit", value: "nanoseconds", unit: "nanosecond" },
];
规范
| Specification |
|---|
| Intl.DurationFormat # sec-Intl.DurationFormat.prototype.formatToParts |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also