Intl.DurationFormat.prototype.format()

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Intl.DurationFormat 实例的 format() 方法根据此 Intl.DurationFormat 对象的区域设置和格式选项来格式化持续时间。

¥The format() method of Intl.DurationFormat instances formats a duration according to the locale and formatting options of this Intl.DurationFormat object.

语法

¥Syntax

js
format(duration)

参数

¥Parameters

duration

要格式化的持续时间对象。它应包括以下部分或全部属性:months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds.

返回值

¥Return value

表示给定 duration 的字符串,根据该 Intl.DurationFormat 对象的区域设置和格式选项进行格式化。

¥A string representing the given duration formatted according to the locale and formatting options of this Intl.DurationFormat object.

注意:大多数时候,format() 返回的格式是一致的。但是,即使在同一语言环境中,输出也可能因实现而异 - 输出变化是设计使然,并且规范允许。它也可能不是你所期望的。例如,字符串可以使用不间断空格或被双向控制字符包围。你不应该将 format() 的结果与硬编码常量进行比较。

¥Note: Most of the time, the formatting returned by format() is consistent. However, the output may vary between implementations, even within the same locale — output variations are by design and allowed by the specification. It may also not be what you expect. For example, the string may use non-breaking spaces or be surrounded by bidirectional control characters. You should not compare the results of format() to hardcoded constants.

示例

¥Examples

使用格式()

¥Using format()

以下示例演示如何使用英语创建持续时间格式化程序。

¥The following example shows how to create a Duration formatter using the English language.

js
const duration = {
  years: 1,
  months: 2,
  weeks: 3,
  days: 3,
  hours: 4,
  minutes: 5,
  seconds: 6,
  milliseconds: 7,
  microseconds: 8,
  nanoseconds: 9,
};

// Without options, style defaults to "short"
new Intl.DurationFormat("en").format(duration);
// "1 yr, 2 mths, 3 wks, 3 days, 4 hr, 5 min, 6 sec, 7 ms, 8 μs, 9 ns"

// With style set to "long"
new Intl.DurationFormat("en", { style: "long" }).format(duration);
// "1 year, 2 months, 3 weeks, 3 days, 4 hours, 5 minutes, 6 seconds, 7 milliseconds, 8 microseconds, 9 nanoseconds"

// With style set to "narrow"
new Intl.DurationFormat("en", { style: "narrow" }).format(duration);
// "1y 2mo 3w 3d 4h 5m 6s 7ms 8μs 9ns"

将 format() 与不同的语言环境和样式一起使用

¥Using format() with different locales and styles

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 set to "en"
new Intl.DurationFormat("en", { style: "short" }).format(duration);
// "1 hr, 46 min and 40 sec"

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

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

// With style set to "digital", locale set to "en", and hours set to "long"
new Intl.DurationFormat("en", { style: "digital", hours: "long" }).format(
  duration,
);
// "1 hour, 46:40"

将 format() 与 fractionalDigits 选项一起使用

¥Using format() with the fractionalDigits option

js
const duration = {
  hours: 11,
  minutes: 30,
  seconds: 12,
  milliseconds: 345,
  microseconds: 600,
};

new Intl.DurationFormat("en", { style: "digital" }).format(duration);
// "11:30:12.3456"

new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 5 }).format(
  duration,
);
// "11:30:12.34560"

new Intl.DurationFormat("en", { style: "digital", fractionalDigits: 3 }).format(
  duration,
);
// "11:30:12.346"

规范

Specification
Intl.DurationFormat
# sec-Intl.DurationFormat.prototype.format

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看