Html HTML5 输入类型时间设置值不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25829686/
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
HTML5 input type time setting values not working
提问by eddard.stark
I have a form with input type time. I am trying to set values. But it is not showing. Here is my code.
我有一个输入类型时间的表单。我正在尝试设置值。但它没有显示。这是我的代码。
<input type="time" name="exit" id="teacher-entry-exit-form-exit-input" class="form-control" value="08:56 AM" required="">
What am I doing wrong?
我究竟做错了什么?
回答by Josh Crozier
It's based on 24-hour time, therefore use value="08:56"
for AM and value="20:56"
for PM.
它基于 24 小时制,因此value="08:56"
用于 AM 和value="20:56"
PM。
<input type="time" value="08:56">
回答by Denys Summers
Stephen, Giri, all you have to do, in order to get AM/PM set, is to convert your 12-hour time string into 24-hour one, before passing it to the value attribute of input [type=time].
Stephen, Giri,为了设置 AM/PM,您只需将 12 小时制时间字符串转换为 24 小时制,然后再将其传递给输入 [type=time] 的 value 属性。
Here is a quick examplehow to convert 12-hour -> 24-hour time string.
这是一个如何转换 12 小时 -> 24 小时时间字符串的快速示例。
const am_pm_to_hours = time => {
let hours = Number(time.match(/^(\d+)/)[1]);
let minutes = Number(time.match(/:(\d+)/)[1]);
const AMPM = time.match(/\s(.*)$/)[1];
if (AMPM.toLowerCase() === "pm" && hours < 12) hours = hours + 12;
if (AMPM.toLowerCase() === "am" && hours == 12) hours = hours - 12;
let sHours = hours.toString();
let sMinutes = minutes.toString();
if (hours < 10) sHours = "0" + sHours;
if (minutes < 10) sMinutes = "0" + sMinutes;
return `${sHours}:${sMinutes}`;
}
hope this helps :)
希望这可以帮助 :)
回答by rahim.nagori
This worked for me :
这对我有用:
<input class="form-control" type="time" name="time" value="<?=date("H:i", strtotime($dbObject['time'])); ?>" />
In this way, one can fetch the time column and display it like this using PHP and mysql.
这样,就可以使用 PHP 和 mysql 获取时间列并像这样显示它。