Date.prototype.toLocaleTimeString() 中的 hourCycle 选项有什么区别
What are the differences between the hourCycle options in Date.prototype.toLocaleTimeString()
toLocaleTimeString() 的 MDN 文档指出 hourCycle
和 hc
选项有四个可能的值:"h11"
、"h12"
、"h23"
、& "h24"
.
两个可能的值让我觉得非常明显(即 "h12"
和 "h24"
),但是另外两个,我不知道它们是做什么的,我的 duckduckfoo/googlefoo 是让我失望!
"h11"
和 "h23"
值代表什么?
我最好的猜测是它们是 0
和 "h12"
和 "h24"
的 1
派生的某种类型,但基础日期戳仍然相同,并且记录的值是相同的,所以如果是这样,区别在哪里?
这应该在 MDN's toLocalTimeString page or ECMAScript's toLocalTimeString page 上记录,或者至少链接到,但事实并非如此。它也确实给我留下了深刻的印象,因为它应该很容易弄清楚,但我没有看到其中的区别,它现在在我的皮肤下爬行!
const now = new Date();
console.log('hourCycle: h11', now.toLocaleTimeString('en-US', { hourCycle: 'h11' }))
console.log('hourCycle: h12', now.toLocaleTimeString('en-US', { hourCycle: 'h12' }))
console.log('hourCycle: h23', now.toLocaleTimeString('en-US', { hourCycle: 'h23' }))
console.log('hourCycle: h24', now.toLocaleTimeString('en-US', { hourCycle: 'h24' }))
我发现dateStyle and timeStyle options for Intl.DateTimeFormat的提案说:
[[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.
英式或美式可能更喜欢h12
:
› new Date(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h12' })
‹ "5/1/2019, 12:00:00 PM"
› new Date(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h11' })
‹ "5/1/2019, 0:00:00 PM"
h24
必须谨慎使用。如果日期部分是前一天的值就好了。
› new Date(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h23' })
‹ "2019/5/1 0:59:59"
› new Date(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h24' })
‹ "2019/5/1 24:59:59"
Compatibility table in MDN 说 Firefox 58 和 Edge 支持这个。
我同意目前很难找到关于 hourCycle
值的 MDN 文档,但我在这里找到了它们:
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.
toLocaleTimeString() 的 MDN 文档指出 hourCycle
和 hc
选项有四个可能的值:"h11"
、"h12"
、"h23"
、& "h24"
.
两个可能的值让我觉得非常明显(即 "h12"
和 "h24"
),但是另外两个,我不知道它们是做什么的,我的 duckduckfoo/googlefoo 是让我失望!
"h11"
和 "h23"
值代表什么?
我最好的猜测是它们是 0
和 "h12"
和 "h24"
的 1
派生的某种类型,但基础日期戳仍然相同,并且记录的值是相同的,所以如果是这样,区别在哪里?
这应该在 MDN's toLocalTimeString page or ECMAScript's toLocalTimeString page 上记录,或者至少链接到,但事实并非如此。它也确实给我留下了深刻的印象,因为它应该很容易弄清楚,但我没有看到其中的区别,它现在在我的皮肤下爬行!
const now = new Date();
console.log('hourCycle: h11', now.toLocaleTimeString('en-US', { hourCycle: 'h11' }))
console.log('hourCycle: h12', now.toLocaleTimeString('en-US', { hourCycle: 'h12' }))
console.log('hourCycle: h23', now.toLocaleTimeString('en-US', { hourCycle: 'h23' }))
console.log('hourCycle: h24', now.toLocaleTimeString('en-US', { hourCycle: 'h24' }))
我发现dateStyle and timeStyle options for Intl.DateTimeFormat的提案说:
[[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.
英式或美式可能更喜欢h12
:
› new Date(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h12' })
‹ "5/1/2019, 12:00:00 PM"
› new Date(2019,4,1,12,0,0).toLocaleString('en-US', { hourCycle: 'h11' })
‹ "5/1/2019, 0:00:00 PM"
h24
必须谨慎使用。如果日期部分是前一天的值就好了。
› new Date(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h23' })
‹ "2019/5/1 0:59:59"
› new Date(2019,4,1,0,59,59).toLocaleString('ja-JP', { hourCycle: 'h24' })
‹ "2019/5/1 24:59:59"
Compatibility table in MDN 说 Firefox 58 和 Edge 支持这个。
我同意目前很难找到关于 hourCycle
值的 MDN 文档,但我在这里找到了它们:
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.