Date.prototype.toLocaleString()
toLocaleString() 方法是 Date 实例的一个方法,它返回一个字符串,该字符串表示此日期在本地时区中的、符合语言习惯的表示形式。在支持 Intl.DateTimeFormat API 的实现中,此方法会代理到 Intl.DateTimeFormat。
每次调用 toLocaleString 时,它都必须在一个庞大的本地化字符串数据库中进行搜索,这可能会效率低下。当该方法以相同的参数多次调用时,最好创建一个 Intl.DateTimeFormat 对象并使用其 format() 方法,因为 DateTimeFormat 对象会记住传递给它的参数,并可能决定缓存数据库的一部分,因此未来的 format 调用可以在更受限制的上下文中搜索本地化字符串。
试一试
const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(event.toLocaleString("en-GB", { timeZone: "UTC" }));
// Expected output: "20/12/2012, 03:00:00"
// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(event.toLocaleString("ko-KR", { timeZone: "UTC" }));
// Expected output: "2012. 12. 20. 오전 3:00:00"
语法
toLocaleString()
toLocaleString(locales)
toLocaleString(locales, options)
参数
locales 和 options 参数自定义函数行为,并允许应用程序指定应使用的语言的格式约定。
在支持 Intl.DateTimeFormat API 的实现中,这些参数与 Intl.DateTimeFormat() 构造函数的参数完全对应。不支持 Intl.DateTimeFormat 的实现则被要求忽略这两个参数,使得所使用的区域设置和返回字符串的格式完全取决于具体实现。
locales可选-
一个带有 BCP 47 语言标签 的字符串,或此类字符串的数组。对应于
Intl.DateTimeFormat()构造函数的locales参数。在不支持
Intl.DateTimeFormat的实现中,将忽略此参数,通常会使用主机的区域设置。 options可选-
一个调整输出格式的对象。对应于
Intl.DateTimeFormat()构造函数的options参数。如果weekday、year、month、day、dayPeriod、hour、minute、second和fractionalSecondDigits都未定义,则year、month、day、hour、minute、second将被设置为"numeric"。在不支持
Intl.DateTimeFormat的实现中,将忽略此参数。
有关这些参数及其使用方法的详细信息,请参阅 Intl.DateTimeFormat() 构造函数。
返回值
一个根据语言特定约定表示给定日期的字符串。
在支持 Intl.DateTimeFormat 的实现中,这等同于 new Intl.DateTimeFormat(locales, options).format(date)。
注意: 大多数情况下,toLocaleString() 返回的格式是一致的。然而,输出在不同实现之间可能会有所不同,即使在相同的区域设置中也是如此——输出差异是设计使然,并受规范允许。它也可能不符合您的预期。例如,字符串可能使用不间断空格或被双向控制字符包围。您不应将 toLocaleString() 的结果与硬编码常量进行比较。
示例
使用 toLocaleString()
此方法的基本用法——不指定 locale 或 options——取决于具体实现,并返回一个基于默认语言环境和时区的字符串,使用默认选项进行格式化。
const date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
console.log(date.toLocaleString());
// "12/11/2012, 7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
检查对 locales 和 options 参数的支持
locales 和 options 参数可能并非在所有实现中都得到支持,因为对国际化 API 的支持是可选的,并且某些系统可能没有必要的数据。对于不支持国际化的实现,toLocaleString() 始终使用系统的语言环境,这可能不是您想要的。由于任何支持 locales 和 options 参数的实现都必须支持 Intl API,因此您可以检查后者是否存在来判断是否支持。
function toLocaleStringSupportsLocales() {
return (
typeof Intl === "object" &&
!!Intl &&
typeof Intl.DateTimeFormat === "function"
);
}
使用语言环境
此示例展示了本地化日期和时间格式的一些变化。为了获得应用程序用户界面所使用的语言的格式,请确保使用 locales 参数指定该语言(以及可能的备用语言)。
const date = new Date(Date.UTC(2012, 1, 2, 3, 0, 0));
// Formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses month-day-year order and 12-hour time with AM/PM
console.log(date.toLocaleString("en-US"));
// "2/1/2012, 7:00:00 PM" (UTC-8 is the previous day)
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(date.toLocaleString("en-GB"));
// "02/02/2012, 03:00:00" (UTC+0 or UTC+1 depending on time of the year)
// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(date.toLocaleString("ko-KR"));
// "2012. 2. 2. 오후 12:00:00"
// Arabic in most Arabic-speaking countries uses Eastern Arabic numerals
console.log(date.toLocaleString("ar-EG"));
// "٢/٢/٢٠١٢ ٥:٠٠:٠٠ ص"
// For Japanese, applications may want to use the Japanese calendar,
// where 2012 was the year 24 of the Heisei era
console.log(date.toLocaleString("ja-JP-u-ca-japanese"));
// "H24/2/2 12:00:00"
// When requesting a language that may not be supported, such as
// Balinese, include a fallback language (in this case, Indonesian)
console.log(date.toLocaleString(["ban", "id"]));
// "2/2/2012 11.00.00"
使用选项
toLocaleString() 提供的结果可以使用 options 参数进行自定义。
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// Request a weekday along with a long date
const options = {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric",
};
console.log(date.toLocaleString("de-DE", options));
// Example output: "Donnerstag, 20. Dezember 2012"
// The exact date may shift depending on your local time zone.
// An application may want to use UTC and make that visible
options.timeZone = "UTC";
options.timeZoneName = "short";
console.log(date.toLocaleString("en-US", options));
// Example output: "Thursday, December 20, 2012 at UTC"
// Sometimes even the US needs 24-hour time
console.log(date.toLocaleString("en-US", { hour12: false }));
// Example output: "12/19/2012, 19:00:00"
// The exact date and time may shift depending on your local time zone.
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-date.prototype.tolocalestring |
| ECMAScript® 2026 国际化 API 规范 # sup-date.prototype.tolocalestring |
浏览器兼容性
加载中…