Intl.DateTimeFormat选项哈希:使用“ 2位数”获取前导零 [英] Intl.DateTimeFormat options hash: Getting leading zeros with '2-digit'

查看:141
本文介绍了Intl.DateTimeFormat选项哈希:使用“ 2位数”获取前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我希望上述调用返回类似 1995年8月9日,星期三,上​​午04:15:00 ,但似乎缺少该小时的前导零。我得到 1995年8月9日,星期三,凌晨4:15:00

I expected the above call to return something like Wednesday, August 09, 1995, 04:15:00 AM but it seems the leading zero for the hour is missing. I get Wednesday, August 09, 1995, 4:15:00 AM

2 -digit 不能解决问题,尽管它似乎适用于每月的某天。 两位数字表示超出我预期的含义还是我做错了吗?

2-digit does not do the trick, though it seems to work for the day of the month. Does 2-digit mean something else than I expect, or am I doing something wrong?

P.S。我在Chrome控制台上进行了测试,无处不在。

P.S. I tested this in the Chrome console, nowhere else.

推荐答案

如果您设置 hour12 属性设置为 false 。即

This will work if you set hour12 property to false. i.e.

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浏览器。

It works for both 2am and also 2pm(14hr). But I know the same should work for 12 hour format too, but it is not. I checked in both chrome and firefox browsers.

当我检查规格时,它定义了算法,可用于实现 Intl.DateTimeFormat 功能。我看到在hour12属性设置为true时处理了许多特殊情况,最后一步之一是

When I checked the specs, it has defined an algorithm which can be used to implement Intl.DateTimeFormat functionality. I saw there were many special cases handled when hour12 property to set true, and one of the last step is



  1. 如果dateTimeFormat具有内部属性[[hour12]],其值为
    true ,则


    • 如果pm为true,则使fv为实现,并且取决于语言环境
      的字符串值,表示后子午线;否则让fv是
      的实现,并且依赖于语言环境的字符串值表示 ante
      meridiem。

    • 用fv替换包含 {ampm},
      的结果子字符串。

  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信息。

So I think, the Intl.DateTimeFormat initially works with date object, later at step(8), it applies this step to put am/pm information.

在此步骤中,可能是 2位数字 Intl.DateTimeFormat <中指定的信息/ code> 未考虑,但必须。我认为这是有效的错误,并且已经提出了< a href = https://code.google.com/p/chromium/issues/detail?id=527926 rel = noreferrer> https://code.google.com/p/chromium/issues/detail? id = 527926 。

During this step, may be 2-digits information specified in Intl.DateTimeFormat is not considered, but it has to be. I think this is a valid bug and it is already raised https://code.google.com/p/chromium/issues/detail?id=527926.

PS:我并不是说问题在于规格,如ECMAScript语言规范,

PS: I'm not saying issue is in specs, as described in the ECMAScript Language Specification,


算法是
,用于精确指定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.

这篇关于Intl.DateTimeFormat选项哈希:使用“ 2位数”获取前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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