Intl.Locale.prototype.calendar

Intl.Locale 实例的 calendar 访问器属性返回此区域设置的日历类型。

¥The calendar accessor property of Intl.Locale instances returns the calendar type for this locale.

描述

¥Description

虽然世界上大多数地区都使用公历,但世界各地也使用几个区域日历时代。calendar 属性的值是在构造时通过区域设置标识符的 ca 键或通过 Intl.Locale() 构造函数的 calendar 选项设置的。如果两者都存在,则后者优先;如果两者都不存在,则该属性的值为 undefined

¥While most of the world uses the Gregorian calendar, there are several regional calendar eras used around the world. The calendar property's value is set at construction time, either through the ca key of the locale identifier or through the calendar option of the Intl.Locale() constructor. The latter takes priority if they are both present; and if neither is present, the property has value undefined.

有关支持的日历类型的列表,请参阅 Intl.Locale.prototype.getCalendars()

¥For a list of supported calendar types, see Intl.Locale.prototype.getCalendars().

calendar 的集合访问器是 undefined。你不能直接更改此属性。

¥The set accessor of calendar is undefined. You cannot change this property directly.

示例

¥Examples

与其他区域设置子标签一样,日历类型可以通过区域设置字符串或构造函数的配置对象参数添加到 Intl.Locale 对象。

¥Like other locale subtags, the calendar type can be added to the Intl.Locale object via the locale string, or a configuration object argument to the constructor.

通过区域设置字符串添加日历类型

¥Adding a calendar type via the locale string

Unicode 语言环境字符串规范 中,日历时代类型是区域设置键 "扩展子标签"。这些子标签添加有关区域设置的附加数据,并使用 -u 扩展添加到区域设置标识符中。因此,日历时代类型可以添加到传递到 Intl.Locale() 构造函数的初始区域设置标识符字符串中。要添加日历类型,请首先将 -u 扩展名添加到字符串中。接下来,添加 -ca 扩展名以指示你要添加日历类型。最后,将日历时代类型添加到字符串中。

¥In the Unicode locale string spec, calendar era types are locale key "extension subtags". These subtags add additional data about the locale, and are added to locale identifiers by using the -u extension. Thus, the calendar era type can be added to the initial locale identifier string that is passed into the Intl.Locale() constructor. To add the calendar type, first add the -u extension to the string. Next, add the -ca extension to indicate that you are adding a calendar type. Finally, add the calendar era type to the string.

js
const locale = new Intl.Locale("fr-FR-u-ca-buddhist");
console.log(locale.calendar); // Prints "buddhist"

通过配置对象参数添加日历类型

¥Adding a calendar type via the configuration object argument

Intl.Locale() 构造函数有一个可选的配置对象参数,它可以包含多种扩展类型中的任何一种,包括日历。将配置对象的 calendar 属性设置为所需的日历纪元,然后将其传递到构造函数中。

¥The Intl.Locale() constructor has an optional configuration object argument, which can contain any of several extension types, including calendars. Set the calendar property of the configuration object to your desired calendar era, and then pass it into the constructor.

js
const locale = new Intl.Locale("fr-FR", { calendar: "buddhist" });
console.log(locale.calendar); // "buddhist"

规范

Specification
ECMAScript Internationalization API Specification
# sec-Intl.Locale.prototype.calendar

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also