Temporal.ZonedDateTime.prototype.toLocaleString()
Temporal.ZonedDateTime 实例的 toLocaleString() 方法返回一个字符串,该字符串以语言敏感的方式表示此日期时间。在支持 Intl.DateTimeFormat API 的实现中,此方法会委托给 Intl.DateTimeFormat,并传递已转换为 Temporal.Instant 的此日期时间(因为 Intl.DateTimeFormat 无法直接格式化 Temporal.ZonedDateTime)。
每次调用 toLocaleString 时,都必须在一个大型本地化字符串数据库中执行搜索,这可能会效率低下。当该方法使用相同的参数多次调用时,最好创建一个 Intl.DateTimeFormat 对象并使用其 format() 方法,因为 DateTimeFormat 对象会记住传递给它的参数,并可能决定缓存数据库的一部分,因此未来的 format 调用可以在更受限制的上下文中搜索本地化字符串。然而,目前 Intl.DateTimeFormat 不支持格式化 Temporal.ZonedDateTime 对象,因此在将其传递给 format() 之前,您必须先将其转换为 Temporal.Instant 对象。
语法
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
参数
locales 和 options 参数自定义函数行为,并允许应用程序指定应使用的语言的格式约定。
在支持 Intl.DateTimeFormat API 的实现中,这些参数与 Intl.DateTimeFormat() 构造函数的参数完全对应。不支持 Intl.DateTimeFormat 的实现返回与 toString() 完全相同的字符串,忽略这两个参数。
locales可选-
一个带有 BCP 47 语言标签 的字符串,或此类字符串的数组。对应于
Intl.DateTimeFormat()构造函数的locales参数。 options可选-
一个调整输出格式的对象。对应于
Intl.DateTimeFormat()构造函数的options参数。如果此日期时间的日历不是"iso8601",则必须提供具有相同值的calendar选项;否则,如果此日期时间的日历是"iso8601",则calendar选项可以是任何值。不得提供timeZone选项,因为它会自动设置为此日期时间的timeZoneId。关于 日期时间组件选项 和样式快捷方式(dateStyle和timeStyle),选项应遵循以下形式之一:- 不提供任何选项:
year、month、day、hour、minute和second将默认设置为"numeric"。 - 提供至少一个
dateStyle或timeStyle:日期时间组件将根据指定的样式和区域设置进行设置。 - 提供一些日期时间组件选项。只有指定的日期时间组件将包含在输出中。
- 不提供任何选项:
有关这些参数及其使用方法的详细信息,请参阅 Intl.DateTimeFormat() 构造函数。
返回值
一个字符串,根据语言特定的约定表示给定的日期时间。
在具有 Intl.DateTimeFormat 的实现中,这等同于 new Intl.DateTimeFormat(locales, { ...options, timeZone: dateTime.timeZoneId }).format(dateTime.toInstant()),其中 options 已按上述方式规范化。
注意: 大多数情况下,toLocaleString() 返回的格式是一致的。然而,输出在不同实现之间可能会有所不同,即使在相同的区域设置中也是如此——输出差异是设计使然,并受规范允许。它也可能不符合您的预期。例如,字符串可能使用不间断空格或被双向控制字符包围。您不应将 toLocaleString() 的结果与硬编码常量进行比较。
异常
RangeError-
如果任何选项无效,则抛出。
TypeError-
如果任何选项的类型不符合预期,则抛出。
示例
使用 toLocaleString()
不指定 locale 的此方法的简单用法以默认区域设置和默认选项返回格式化字符串。
const zdt = Temporal.ZonedDateTime.from(
"2021-08-01T12:34:56-04:00[America/New_York]",
);
console.log(zdt.toLocaleString()); // 8/1/2021, 12:34:56 PM EDT (assuming en-US locale)
如果日期的日历与区域设置的默认日历不匹配,并且日期的日历不是 iso8601,则必须提供具有相同值的显式 calendar 选项。
const zdt = Temporal.ZonedDateTime.from(
"2021-08-01T12:34:56+09:00[Asia/Tokyo][u-ca=japanese]",
);
// The ja-JP locale uses the Gregorian calendar by default
zdt.toLocaleString("ja-JP", { calendar: "japanese" }); // R3/8/1 12:34:56 JST
使用带选项的 toLocaleString()
您可以通过提供 options 参数来自定义日期中包含在输出中的部分。
const zdt = Temporal.ZonedDateTime.from(
"2021-08-01T12:34:56+09:00[Asia/Tokyo][u-ca=japanese]",
);
zdt.toLocaleString("ja-JP", {
calendar: "japanese",
dateStyle: "full",
timeStyle: "full",
}); // 令和3年8月1日日曜日 12時34分56秒 日本標準時
zdt.toLocaleString("ja-JP", {
calendar: "japanese",
year: "numeric",
month: "long",
hour: "numeric",
timeZoneName: "shortGeneric",
}); // 令和3年8月 12時 JST
zdt.toLocaleString("ja-JP", {
calendar: "japanese",
year: "numeric",
hour: "numeric",
minute: "numeric",
}); // 令和3年 12:34
规范
| 规范 |
|---|
| Temporal # sec-temporal.zoneddatetime.prototype.tolocalestring |
浏览器兼容性
加载中…