Intl.Locale.prototype.hourCycle - JavaScript 编辑

The Intl.Locale.prototype.hourCycle property is an accessor property that returns the time keeping format convention used by the locale.

Description

There are 2 main types of time keeping conventions (clocks) used around the world: the 12 hour clock and the 24 hour clock. The hourCycle property makes it easier for JavaScript programmers to access the clock type used by a particular locale. Like other additional locale data, hour cycle type is an extension subtag, which extends the data contained in a locale string. The hour cycle type can have several different values, which are listed in the table below.

Valid hour cycle types

Hour cycle typeDescription
h12Hour system using 1–12; corresponds to 'h' in patterns. The 12 hour clock, with midnight starting at 12:00 am.
h23Hour system using 0–23; corresponds to 'H' in patterns. The 24 hour clock, with midnight starting at 0:00.
h11Hour system using 0–11; corresponds to 'K' in patterns. The 12 hour clock, with midnight starting at 0:00 am.
h24Hour system using 1–24; corresponds to 'k' in pattern. The 24 hour clock, with midnight starting at 24:00.

Examples

These examples will show you how to add hour cycle data to your Locale object.

Adding an hour cycle via the locale string

In the Unicode locale string spec, the hour cycle is a locale string "extension subtag". These subtags add additional data about the locale, and are added to locale identifiers by using the -u extension key. Thus, the hour cycle type can be added to the inital locale identifier string that is passed into the Locale constructor. To add the hour cycle type, first add the -u extension key to the string. Next, add the -hc extension key to indicate that you are adding an hour cycle. Finally, add the hour cycle type to the string.

let fr24hour = new Intl.Locale("fr-FR-u-hc-h23");
console.log(fr24hour.hourCycle); // Prints "h23"

Adding an hour cycle via the configuration object argument

The Intl.Locale constructor has an optional configuration object argument, which can contain any of several extension types, including hour cycle types. Set the hourCycle property of the configuration object to your desired hour cycle type, and then pass it into the constructor.

let us12hour = new Intl.Locale("en-US", {hourCycle: "h12"});
console.log(us12hour.hourCycle); // Prints "h12"

Specifications

Specification
ECMAScript Internationalization API (ECMA-402)

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:79 次

字数:5382

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文