Intl.DateTimeFormat不关心’2-digit’或’numeric’。

huangapple go评论83阅读模式
英文:

Intl.DateTimeFormat does not bother about '2-digit' or 'numeric'

问题

I want to use IntlDateTimeFormat to provide leading zeros.

My code is:

const timeFormatter = Intl.DateTimeFormat(undefined, { minute: '2-digit' });

const date = new Date();
date.setMinutes(4);

console.log(timeFormatter.format(date)); // PRINTS: 4 and not 04

However, when I add second: '2-digit' to the options object. Then it works fine, but also prints seconds (Yes, I can remove it using replace).

What's the difference between '2-digit' and 'numeric' then?

I am ignoring padStart, as of now.

I tried to change the configuration back to 'numeric' instead of '2-digit'. There seems no difference.

英文:

I want to use IntlDateTimeFormat to provide leading zeros.
My code is:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const timeFormatter = Intl.DateTimeFormat(undefined,{minute: &#39;2-digit&#39;});

const date = new Date();
date.setMinutes(4);

console.log(timeFormatter.format(date));  // PRINTS: 4 and not 04

<!-- end snippet -->

However, when I add second: &#39;2-digit&#39; to the options object. Then it works fine, but also prints seconds (Yes, I can remove it using replace).

What's the difference between '2-digit' and 'numeric' then?

I am ignoring padStart, as of now.

I tried to change the configuration back to 'numeric' instead of '2-digit'. There seems no difference

答案1

得分: 2

"2-digit" 和 "numeric" 之间的区别是什么?

并非所有选项都适用于所有串行化的选项组合。

仅对于支持在特定语言中序列化2位数分钟的格式,该设置控制分钟是否以1位数或2位数表示。

Intl.DateTimeFormat.prototype.format()生成的格式化字符串既依赖于实现又依赖于语言,因此不适合生成需要精确匹配模式的字符串。

其他组合可能也会产生意料之外的字符串:

const date = new Date(2023, 4, 10, 16, 4, 8);

const printFormatted = (opts) => console.log(new Intl.DateTimeFormat(undefined, opts).format(date));

printFormatted({ minute: "2-digit" });
printFormatted({ minute: "numeric", second: "numeric" });
printFormatted({ minute: "numeric", weekday: "long" });
printFormatted({ minute: "2-digit", weekday: "long" });

根据您问题中的示例代码:如果您的目标是将1位数或2位数的整数转换为具有前导零的2位数字符串,那么String.prototype.padStart() 是惯用的解决方案:

const date = new Date(2023, 4, 10, 16, 4, 8);

const zeroFour = String(date.getMinutes()).padStart(2, "0");

console.log(zeroFour); // 04
英文:

> What's the difference between '2-digit' and 'numeric'?

Not all options apply to all serializations for all option combinations.

For (only) the formats which are implemented to support serializing 2-digit minutes in a given language, that setting controls whether or not the minutes will be represented as 1 or 2 digits.

Formatted strings produced by Intl.DateTimeFormat.prototype.format() are both implementation- and language-dependent, so they are not appropriate for producing strings which need to match exact schemas.

Other combinations might also produce strings you don't expect:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const date = new Date(2023, 4, 10, 16, 4, 8);

const printFormatted = (opts) =&gt; console.log(new Intl.DateTimeFormat(undefined, opts).format(date));

printFormatted({ minute: &quot;2-digit&quot; });
printFormatted({ minute: &quot;numeric&quot;, second: &quot;numeric&quot; });
printFormatted({ minute: &quot;numeric&quot;, weekday: &quot;long&quot; });
printFormatted({ minute: &quot;2-digit&quot;, weekday: &quot;long&quot; });

<!-- end snippet -->

Based on the example code in your question: if your goal is to convert a 1-digit or 2-digit integer to a 2-digit string with a leading 0, then String.prototype.padStart() is the idiomatic solution:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const date = new Date(2023, 4, 10, 16, 4, 8);

const zeroFour = String(date.getMinutes()).padStart(2, &quot;0&quot;);

console.log(zeroFour); // 04

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月11日 00:41:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定