我正在编写一个简单的 JS 应用程序,它需要以 12 小时格式显示时间。为此,我使用了 Date
类的 toLocaleString
。但不幸的是,它返回 12:05 的时间为 00:05 pm 和 00:05 为 00:05 am 我希望它分别显示 12:05 pm 和 12:05 am。
下面是我使用该函数的方式:
console.log(new Date("2020-08-15 12:15").toLocaleString("en-GB", { dateStyle: "medium", timeStyle: "short", hour12: true}));
console.log(new Date("2020-08-15 00:15").toLocaleString("en-GB", { dateStyle: "medium", timeStyle: "short", hour12: true}))
是否有解决方法或任何其他功能可以正确显示 12 点钟的时间?
Problem occurs only at 12 o'clock and something past 12
最佳答案
定义格式细节时有更多选项。即:
hourCycle:'h11'
hourCycle:'h12'
hourCycle:'h23'
hourCycle:'h24'
这里有更多选项...
https://www.w3schools.com/jsref/jsref_tolocalestring.asp
或者,考虑使用澳大利亚格式,它无需任何额外编码即可满足您的需求...:)
en-AU: 15/08/2020, 10:43:45 am
关于javascript - toLocaleString 给出错误的 12 小时时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63423553/