编码
const d = new Date('2021-08-04T18:33:43.0000000Z')
.toLocaleString('en-US', {hour12: false, timeZone: "Asia/Kolkata"})
console.log(d)
正在提供结果为
"8/5/2021, 24:03:43"
.此处“24”小时应显示为“00”小时。这是一个
toLocaleString
功能问题?我需要使用上述语法,因为日期格式应该是浏览器语言日期格式,时间应该是 24 小时格式。
对于其他语言,它工作正常,例如 GB
const d = new Date('2021-08-04T18:33:43.0000000Z')
.toLocaleString('en-GB', {hour12: false, timeZone: "Asia/Kolkata"})
console.log(d)
将提供结果为
"05/08/2021, 00:03:43"
.那么有人可以帮助我如何使用
toLocaleString
获得正确的 24 小时格式时间吗?功能?
最佳答案
好像是INTL ECMA 规范中的一个bug,应该在ECMA2021 中修复。
这可能是 Chromium 唯一的问题
同时您可以添加 hourCycle: 'h23'
从 h24
到您的参数强制 00当 hour12: false
时,是某些语言环境的默认值被指定
https://github.com/moment/luxon/issues/726
The root cause of this issue comes from this change in Chromium. It introduces support for the hourCycle option in Intl.DateTimeFormat.
The four supported values are "h11", "h12", "h23", or "h24" (MDN) and the difference between h23 and h24 is precisely if midnight should be displayed as 00 (h23) or 24 (h24)
What are the differences between the hourCycle options in Date.prototype.toLocaleTimeString()
关于javascript - Date.toLocaleString ('en-US' , {hour12 : false}) is providing midnight as 24,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68646411/