javascript - Date.toLocaleString ('en-US' , {hour12 : false}) is providing midnight as 24

标签 javascript date

编码

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/

相关文章:

javascript - 有没有办法计算一个文件夹中有多少个文件,然后将其导入到变量中?

javascript - 使用 javascript 数组添加节点

date - parse_dates 不适用于默认日期时间格式

r - 将儒略日期转换为 PosixCt 日期

date - 如何将当前日期或时间添加到文件中每一行的末尾?

date - 如何将天数添加到 Cypher 中的日期属性?

javascript - 如何使用微数据提取/解析 HTML

javascript - Bootstrap数据表搜索功能

javascript - 监听 Ember 组件外部点击的正确方法是什么?

javascript - Javascript 中的 -1 小时时间戳