Date.prototype.toLocaleTimeString()中的HourCycle选项之间有什么区别 [英] What are the differences between the hourCycle options in Date.prototype.toLocaleTimeString()

查看:41
本文介绍了Date.prototype.toLocaleTimeString()中的HourCycle选项之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

toLocaleTimeString()的MDN文档指示 hourCycle hc 选项具有四个可能的值:"h11" "h12"" h23",&"h24" .

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

其中两个可能的值使我感到很明显(即"h12" "h24" ),但是其他两个,我不知道它们做什么而我的duckduckfoo/googlefoo使我失望了!

Two of the possible values strike me as super obvious (i.e. "h12" and "h24"), but the other two, I have no idea what they do and my duckduckfoo/googlefoo is failing me!

什么分别代表"h11" "h23" 值?

我最好的猜测是,它们是"h12" "h24" ,但是底层的日期戳仍然是相同的,并且记录的值是相同的,因此,如果是这样,差异在哪里?

My best guess is that they are some type of 0 vs 1 derivations of "h12" and "h24", but the underlying date stamp is still the same, and the value logged is the same, so if this is it, where is the difference?

应在 MDN的toLocalTimeString页面 ECMAScript的toLocalTimeString页面,但事实并非如此.它确实让我印象深刻,因为它应该很容易弄清楚,但是我没有看到区别,它现在正在我的皮肤下爬行!

This should be documented, or at least linked to, on MDN's toLocalTimeString page or ECMAScript's toLocalTimeString page, but it's not. It also really strikes me as something that should be simple to figure out, and yet I’m not seeing the difference, and it’s now crawling under my skin!

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' }))

推荐答案

我发现

[[HourCycle]]是一个字符串值,指示是12小时制("h11" "h12" )还是24小时制("h23" "h24" )."h11" "h23" 从小时0开始,分别上升到11和23."h12" "h24" 从1小时开始,一直到12和24.[[HourCycle]]仅在[[Hour]]不是未定义.

[[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 :

English or US style may prefer 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 必须谨慎使用.如果日期部分是前一天的值,那会很好.

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

› 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"

MDN中的兼容性表表示Firefox 58和Edge支持此功能.

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

这篇关于Date.prototype.toLocaleTimeString()中的HourCycle选项之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆