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

Temporal.Instant.prototype.toLocaleString()

· 52CE 我爱测

The **`toLocaleString()`** method of Temporal.Instant instances returns a string with a language-sensitive representation of this instant. In implementations with `Intl.DateTimeFormat` API support, this method delegates to `Intl.DateTimeFormat`.

The toLocaleString() method of Temporal.Instant instances returns a string with a language-sensitive representation of this instant. In implementations with Intl.DateTimeFormat API support, this method delegates to Intl.DateTimeFormat.

Every time toLocaleString is called, it has to perform a search in a big database of localization strings, which is potentially inefficient. When the method is called many times with the same arguments, it is better to create an Intl.DateTimeFormat object and use its Intl/DateTimeFormat/format method, because a DateTimeFormat object remembers the arguments passed to it and may decide to cache a slice of the database, so future format calls can search for localization strings within a more constrained context.

Syntax

toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)

Parameters

The locales and options parameters customize the behavior of the function and let applications specify the language whose formatting conventions should be used.

In implementations that support the Intl.DateTimeFormat API, these parameters correspond exactly to the Intl.DateTimeFormat() constructor's parameters. Implementations without Intl.DateTimeFormat support return the exact same string as Temporal/Instant/toString, ignoring both parameters.

  • locales
    • : A string with a BCP 47 language tag, or an array of such strings. Corresponds to the locales parameter of the Intl.DateTimeFormat() constructor.
  • options
    • : An object adjusting the output format. Corresponds to the options parameter of the Intl.DateTimeFormat() constructor.

See the Intl.DateTimeFormat() constructor for details on these parameters and how to use them.

Return value

A string representing the given instant according to language-specific conventions.

In implementations with Intl.DateTimeFormat, this is equivalent to new Intl.DateTimeFormat(locales, options).format(instant).

[!NOTE] Most of the time, the formatting returned by toLocaleString() 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 toLocaleString() to hardcoded constants.

Examples

Using toLocaleString()

Basic use of this method without specifying a locale returns a formatted string in the default locale and with default options.

const instant = Temporal.Instant.from("2021-08-01T12:34:56Z");

console.log(instant.toLocaleString()); // 8/1/2021, 12:34:56 AM (assuming en-US locale and device in UTC time zone)

Specifications

Browser compatibility

See also

  • Temporal.Instant
  • Intl.DateTimeFormat
  • Temporal/Instant/toJSON
  • Temporal/Instant/toString

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

资料来源与适用范围

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

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

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