24 格式的 HTML 输入时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32609407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 18:14:23  来源:igfitidea点击:

HTML input time in 24 format

html

提问by Vikas Singh

I am using below HTML tag:

我正在使用以下 HTML 标签:

<input type="time"  />

In Chrome, Its showing PM/AM format, can we restrict to display 24 format always. I dont want to use HTML5 tag

在 Chrome 中,它显示 PM/AM 格式,我们可以限制总是显示 24 格式。我不想使用 HTML5 标签

采纳答案by G?khan Kurt

As stated in this answernot all browsers support the standard way. It is not a good idea to use for robust user experience. And if you use it, you cannot ask too much.

本答案所述,并非所有浏览器都支持标准方式。用于强大的用户体验并不是一个好主意。如果你使用它,你不能要求太多。

Instead use time-picker libraries. For example: TimePicker.jsis a zero dependency and lightweight library. Use it like:

而是使用时间选择器库。例如:TimePicker.js是一个零依赖的轻量级库。像这样使用它:

var timepicker = new TimePicker('time', {
  lang: 'en',
  theme: 'dark'
});
timepicker.on('change', function(evt) {
  
  var value = (evt.hour || '00') + ':' + (evt.minute || '00');
  evt.element.value = value;

});
<script src="https://cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.js"></script>
<link href="https://cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.css" rel="stylesheet"/>

<div>
  <input type="text" id="time" placeholder="Time">
</div>

回答by Vikas NS

<!DOCTYPE html>
<html>
    <body>
        Time: <input type="time" id="myTime" value="16:32:55">

 <p>Click the button to get the time of the time field.</p>

 <button onclick="myFunction()">Try it</button>

 <p id="demo"></p>

 <script>
     function myFunction() {
         var x = document.getElementById("myTime").value;
         document.getElementById("demo").innerHTML = x;
     }
 </script>
    </body>
</html>

I found that by setting value field (not just what is given below) time input will be internally converted into the 24hr format.

我发现通过设置值字段(不仅仅是下面给出的)时间输入将在内部转换为 24 小时格式。

回答by Pranav Nutalapati

There isn't a way to do this yetbecause the type=timeelement is really new.

目前还没有办法做到这一点因为该type=time元素确实是新的。

https://www.w3.org/TR/2012/WD-html-markup-20120315/input.time.htmlhas a list of valid attributes...

https://www.w3.org/TR/2012/WD-html-markup-20120315/input.time.html有一个有效属性列表......

EDITFixed broken link

编辑固定断开的链接

回答by Bill Gauvey

You can't do it with the HTML5 input type. There are many libs available to do it, you can use momentjs or some other jQuery UI components for the best outcome.

你不能用 HTML5 输入类型来做到这一点。有很多库可以做到这一点,您可以使用 momentjs 或其他一些 jQuery UI 组件以获得最佳结果。

回答by Muhammad Haroon

Tested!

测试!

In Windows -> control panel -> Region -> Additional Settings -> Time -> Short Time:

在 Windows -> 控制面板 -> 区域 -> 其他设置 -> 时间 -> 短时间:

Format your time as HH:mm

将您的时间格式化为 HH:mm

in the format

在格式

hh = 12 hours

hh = 12 小时

HH = 24 hours

HH = 24 小时

mm = minutes

毫米 = 分钟

tt = AM or PM

tt = 上午或下午

so to get the required result the format should be HH:mm and not hh:mm tt

所以要获得所需的结果格式应该是 HH:mm 而不是 hh:mm tt