如何获得小时的正确输出:带有AM/PM的toLocaleString("en-US")? [英] How to get correct output of hour: "2-digit" for toLocaleString("en-US") with AM/PM?

查看:69
本文介绍了如何获得小时的正确输出:带有AM/PM的toLocaleString("en-US")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 toLocaleString()MDN文档选项 hour:"2位数" 应该返回2位数的小时表示形式,但是如果语言环境为 en-US ,则仅返回1位数.>,其格式为 AM/PM .(更新:提及AM/PM)

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);

是否有一种变通方法或另一种简便的方法来获取美国语言环境的两位数小时,显示AM和PM?

Is there a workaround or another easy way to get the 2-digit hour for the US locale, displaying the AM and PM?

推荐答案

您只需在选项中明确禁用12小时制:

You 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);

2位数参数可能与填充有关,但我认为这不是绝对必要的.我会考虑将其删除.

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);

这篇关于如何获得小时的正确输出:带有AM/PM的toLocaleString("en-US")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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