Intl.DateTimeFormat 选项散列:使用“2 位数字”获取前导零

Intl.DateTimeFormat options hash: Getting leading zeros with '2-digit'

Intl.DateTimeFormat('en-US', {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
}).format(807959700000)

我预计上面对 return 的调用类似于 Wednesday, August 09, 1995, 04:15:00 AM,但似乎缺少小时的前导零。我得到 Wednesday, August 09, 1995, 4:15:00 AM

2-digit 并不能解决问题,尽管它似乎适用于每月的某一天。 2-digit 的意思是否与我预期的不同,还是我做错了什么?

P.S。我在 Chrome 控制台测试了这个,别处没有。

如果您将 hour12 属性 设置为 false,这将起作用。即

Intl.DateTimeFormat('en-US', {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false     // setting 24 hour format 
}).format(807959700000);

它适用于凌晨 2 点和下午 2 点(14 小时)。但我知道同样适用于 12 小时格式,但事实并非如此。我检查了 chrome 和 firefox 浏览器。

当我查看规范时,它定义了一个 algorithm 可用于实现 Intl.DateTimeFormat 功能。我看到 hour12 属性 设置为 true 时处理了很多特殊情况,最后一步是

  1. If dateTimeFormat has an internal property [[hour12]] whose value is true, then
    • If pm is true, then let fv be an implementation and locale dependent String value representing “post meridiem”; else let fv be an implementation and locale dependent String value representing “ante meridiem”.
    • Replace the substring of result that consists of "{ampm}", with fv.

所以我认为,Intl.DateTimeFormat 最初与日期对象一起工作,稍后在步骤 (8) 中,它应用此步骤来放置 am/pm 信息。

在这一步中,可能2-digitsIntl.DateTimeFormat中指定的信息不考虑,但必须。我认为这是有效的 bug and it is already raised https://code.google.com/p/chromium/issues/detail?id=527926.


PS:我并不是说问题出在规范中,如 ECMAScript 语言规范中所述,

Algorithms are used to precisely specify the required semantics of ECMAScript constructs, but are not intended to imply the use of any specific implementation technique.

这仍然不适用于 Chrome 版本 79Node.js v13.7.0!

他们都忽略了 2-digit 格式。这是一个快速测试用例:

d = new Date('2020-01-01T03:04:09Z') 
new Intl.DateTimeFormat('en', { minute: '2-digit'}).format(d) //"4"

//should return "04" but it actually returns "4"

当您看到它已修复时,请对此 post 发表评论。


2021 年 6 月 22 日更新:

还是不行 Chrome Version 91.0.4472.106 Node.Js v14.15.4