gpt4 book ai didi

javascript - 为什么 2 位数字不适用于 Date.toLocaleTimeString?

转载 作者:行者123 更新时间:2023-11-30 09:14:28 26 4
gpt4 key购买 nike

我发誓我这样做是对的,正如描述的那样on MDN :

(new Date()).toLocaleTimeString(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})

但这是输出 "3:13:22 AM"....小时不是两位数。我究竟做错了什么?我很累。

最佳答案

hour:'2-digit' 属性似乎被主要的 Chrome、Firefox、Safari 和 NodeJS 忽略了。相反,他们应用选项 hour12: 值(即 12 或 24 小时制):

hour12:true results in "3:00:00 PM" since no one really writes "03:00:00 PM" with the presence of PM. Doing so may confuse others into thinking that this is 24-hour notation (i.e. 3 AM) if one reads quickly and misses the PM at the end.

hour12:false results in "15:00:00" and "PM" is no longer needed.

因此在您的示例中,如果您想要 24 小时制,只需使用:

let date = (new Date("17 May 2019 3:13:22 AM")).toLocaleTimeString(undefined, {
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
})

console.log(date); //03:13:22

关于javascript - 为什么 2 位数字不适用于 Date.toLocaleTimeString?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56181412/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com