从HTML时间输入获取AM/PM [英] Get AM/PM from HTML time input
问题描述
首先,我知道IE10或更早版本和Firefox不支持<input type="time">
,但这只是出于测试目的,我将在另一时间进行跨浏览器的适当更改.
First, I know that <input type="time">
is not supported by IE10 or earlier, and Firefox, but this is only for testing purposes and I will make appropriate, cross-browser changes at another time.
目前,我有一个HTML时间输入字段.我试图从中提取值,包括小时,分钟和AM/PM.我可以显示小时和分钟,但不包括AM/PM指示.
For the time being, I have a HTML time input field. I am trying to extract the values from it including the hour, minutes and AM/PM. I can display the hour and minutes, but an AM/PM indication is not including.
我的输入如下:
<input type="time" name="end-time" id="end-time">
我通过alert()
打印输出值,如下所示:
I print out via alert()
the input value as follows:
alert(document.getElementById('end-time').value);
输入后,我会收到一条警报,例如:
Upon input I receive an alert such as:
01:34
.
请注意,alert()
中未包含AM/PM.
Notice that an AM/PM is not included in the alert()
.
如何从通过Javascript输入的HTML时间中获取AM/PM值?
How can I get the AM/PM value from a HTML time input via Javascript?
推荐答案
据我所知,我们无法直接从子午线格式"AM/PM"的输入中获取值.相反,我们可以编写自己的逻辑,以AM/PM格式转换值并存储/显示.
As far as i know, We cannot get the value from the input directly in the meridian format of 'AM/PM'. Instead, we can write our own logic of converting the value in the AM/PM format and store/display it.
下面是它的实现.
var inputEle = document.getElementById('timeInput');
function onTimeChange() {
var timeSplit = inputEle.value.split(':'),
hours,
minutes,
meridian;
hours = timeSplit[0];
minutes = timeSplit[1];
if (hours > 12) {
meridian = 'PM';
hours -= 12;
} else if (hours < 12) {
meridian = 'AM';
if (hours == 0) {
hours = 12;
}
} else {
meridian = 'PM';
}
alert(hours + ':' + minutes + ' ' + meridian);
}
<input type="time" onchange="onTimeChange()" id="timeInput" />
这篇关于从HTML时间输入获取AM/PM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!