Skip to content Skip to sidebar Skip to footer

What Are The Differences Between The Hourcycle Options In Date.prototype.tolocaletimestring()

MDN documentation for toLocaleTimeString() indicates that the hourCycle and hc options have four possible values: 'h11', 'h12', 'h23', & 'h24'. Two of the possible values strik

Solution 1:

I found that the proposal of dateStyle and timeStyle options for Intl.DateTimeFormat says:

[[HourCycle]] is a String value indicating whether the 12-hour format ("h11", "h12") or the 24-hour format ("h23", "h24") should be used. "h11" and "h23" start with hour 0 and go up to 11 and 23 respectively. "h12" and "h24" start with hour 1 and go up to 12 and 24. [[HourCycle]] is only used when [[Hour]] is not undefined.

English or US style may prefer h12:

newDate(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h12' })
‹ "5/1/2019, 12:00:00 PM"newDate(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h11' })
‹ "5/1/2019, 0:00:00 PM"

h24 must be used with caution. It would have been nice if the date part was the value one day before.

newDate(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h23' })
‹ "2019/5/1 0:59:59"newDate(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h24' })
‹ "2019/5/1 24:59:59"

Compatibility table in MDN says Firefox 58 and Edge supports this.

Solution 2:

I agree it's currently difficult to find MDN's documentation on hourCycle values, but I found them here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Locale/hourCycle

Web technology for developers > JavaScript > JavaScript reference > Standard built-in objects > Intl.Locale > Intl.Locale.prototype.hourCycle

[…]

h12: Hour system using 1–12; corresponds to 'h' in patterns. The 12 hour clock, with midnight starting at 12:00 am.

h23: Hour system using 0–23; corresponds to 'H' in patterns. The 24 hour clock, with midnight starting at 0:00.

h11: Hour system using 0–11; corresponds to 'K' in patterns. The 12 hour clock, with midnight starting at 0:00 am.

h24: Hour system using 1–24; corresponds to 'k' in pattern. The 24 hour clock, with midnight starting at 24:00.

Post a Comment for "What Are The Differences Between The Hourcycle Options In Date.prototype.tolocaletimestring()"