admin管理员组文章数量:1022405
According to the toLocaleString() MDN Documentation the option hour: "2-digit"
should return a 2 digit representation of the hour, but it returns only 1 digit if the locale is en-US and the format is AM/PM. (Update: AM/PM mention)
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour: "2-digit", minute: "2-digit"});
console.log(d);
According to the toLocaleString() MDN Documentation the option hour: "2-digit"
should return a 2 digit representation of the hour, but it returns only 1 digit if the locale is en-US and the format is AM/PM. (Update: AM/PM mention)
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour: "2-digit", minute: "2-digit"});
console.log(d);
Is there a workaround or another easy way to get the 2-digit hour for the US locale, displaying the AM and PM?
Share Improve this question edited May 5, 2019 at 15:21 Peter asked May 5, 2019 at 0:58 PeterPeter 311 silver badge3 bronze badges 2- Date.prototype.toLocaleString is somewhat quirky. A library will often give better results, or at least you can chose one that behaves as you require. :-) BTW, in Safari your code snippet returns Invalid Date, see Why does Date.parse give incorrect results? – RobG Commented May 5, 2019 at 5:24
- Thank you @RobG ! – Peter Commented May 5, 2019 at 15:23
1 Answer
Reset to default 5You just have to explicitly disable the 12 hour representation in the options :
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour: "2-digit", minute: "2-digit", hour12: false});
console.log(d);
The 2 digits parameter might be related to padding, but I don't think it's absolutely necessary. I would consider removing it.
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour12: false});
console.log(d);
According to the toLocaleString() MDN Documentation the option hour: "2-digit"
should return a 2 digit representation of the hour, but it returns only 1 digit if the locale is en-US and the format is AM/PM. (Update: AM/PM mention)
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour: "2-digit", minute: "2-digit"});
console.log(d);
According to the toLocaleString() MDN Documentation the option hour: "2-digit"
should return a 2 digit representation of the hour, but it returns only 1 digit if the locale is en-US and the format is AM/PM. (Update: AM/PM mention)
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour: "2-digit", minute: "2-digit"});
console.log(d);
Is there a workaround or another easy way to get the 2-digit hour for the US locale, displaying the AM and PM?
Share Improve this question edited May 5, 2019 at 15:21 Peter asked May 5, 2019 at 0:58 PeterPeter 311 silver badge3 bronze badges 2- Date.prototype.toLocaleString is somewhat quirky. A library will often give better results, or at least you can chose one that behaves as you require. :-) BTW, in Safari your code snippet returns Invalid Date, see Why does Date.parse give incorrect results? – RobG Commented May 5, 2019 at 5:24
- Thank you @RobG ! – Peter Commented May 5, 2019 at 15:23
1 Answer
Reset to default 5You just have to explicitly disable the 12 hour representation in the options :
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour: "2-digit", minute: "2-digit", hour12: false});
console.log(d);
The 2 digits parameter might be related to padding, but I don't think it's absolutely necessary. I would consider removing it.
let d = new Date("2019-05-03 15:00:00").toLocaleString("en-US", {hour12: false});
console.log(d);
本文标签:
版权声明:本文标题:javascript - How to get correct output of hour: "2-digit" for toLocaleString("en-US") with A 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745573247a2156865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论