我创建了两个文本框,当用户按下tab键时,文本框的值将转换为24小时格式,这将需要时间作为用户的输入我已经写了一个方法
function getFormattedTime(value) {
var time = j$.trim(value);
var len = time.length;
var result = "";
var pos = time.indexOf(":");
if (pos < 0) {
pos = time.indexOf(".");
}
if (time.indexOf('m') < 0 && pos < 0 && parseInt(time) > 12
&& parseInt(time) < 12) {
time = time + 'm';
}
if (time.indexOf('m') > 0) {
time = parseFloat(time.substring(0, time.indexOf('m')));
if (isNaN(time)) {
window
.alert("Hours entered is invalid [00:00] or [00.00] or [00h] or [00m]");
return "";
} else {
if (time > 59) {
result = parseInt(time / 60);
result = result + ":" + (time - (result * 60));
} else {
result = "00:"
+ (parseFloat(time) <= 9 ? "0" + time : time);
}
}
return result;
}
var hour = "00";
if (time.charAt(0) == '0') {
hour = '0'
+ (parseFloat(time.substring(1, 2)) ? parseFloat(time
.substring(1, 2)) : '0');
} else {
hour = (parseFloat(time.substring(0, 2)) ? parseFloat(time
.substring(0, 2)) : '00');
hour = (hour > 24 ? 24 : (hour < 10 ? (hour == 0 ? '00' : '0'
+ hour) : hour));
}
var mins = "00";
if (pos != -1) {
if (time.charAt(pos + 1) == '0') {
mins = '0'
+ (parseFloat(time.substring(pos + 2, pos + 3))
? parseFloat(time.substring(pos + 2, pos + 3))
: '0');
} else {
mins = (parseFloat(time.substring(pos + 1, 5))
? parseFloat(time.substring(pos + 1, 5))
: '00');
mins = (mins >= 60 ? 59 : (mins < 10 ? (mins >= 6 ? 59 : mins
+ '0') : mins));
mins = (hour == 24 ? 45 : mins);
}
}
result = hour + ":" + mins;
return result;
}
它对以下格式的运行良好
1.)0-00:002.)9.3-09:303.)13.3-13:304.)9-09:00
但当我进入时
1352它应该给我13:52,但它给了13:00我哪里做错了有人帮忙吗?
当pos
为-1时会发生什么?当pos
为-1时,剩下的是:
var mins = "00";
//...
result = hour + ":" + mins;
pos
是如何变成-1的?只要time
不包含冒号或句点:
var pos = time.indexOf(":");
if (pos < 0) {
pos = time.indexOf(".");
}
特别地,当time
是1352时,mins
将是"00"
。当没有冒号或句点时,您需要在最后一个if
上使用else
分支来处理这种情况。
在这种情况下,您将mins设置为字符串"00"。因此,在第二行中,您将一个字符串与一个数字进行多次比较。最终将分钟设置为"00"。
mins = (parseFloat(time.substring(pos + 1, 5)) ? parseFloat(time.substring(pos + 1, 5))
: '00');
mins = (mins >= 60 ? 59 : (mins < 10 ? (mins >= 6 ? 59 : mins. + '0') : mins));
mins = (hour == 24 ? 45 :
怎么样
此处演示
<style>
#time { width:50px; border:0; text-align:right}
select {text-align:right}
</style>
<script>
var mil = false; // use am/pm
window.onload=function() {
var hour = document.getElementById("hour");
var min = document.getElementById("minutes");
var ampm = document.getElementById("ampm");
for (var i=0;i<24;i++) {
var val = i<10&&mil?"0"+i:i;
if (!mil && val>12) val-=12;
hour.options[i]=new Option(val,i);
}
for (var i=0;i<60;i++) {
var val = i<10?"0"+i:i;
min.options[i]=new Option(val,i);
}
hour.onchange=function() {
if (!mil) ampm.innerHTML=(hour.selectedIndex)<12?"am":"pm";
document.getElementById("time").value=hour.options[hour.selectedIndex].text+":"+min.value;
}
min.onchange=function() {
hour.onchange();
}
var now = new Date();
hour.selectedIndex=now.getHours();
min.selectedIndex=now.getMinutes();
hour.onchange();
}
</script>
<select id="hour" name="hour"></select>:<select id="minutes" name="minutes"></select>
<input type="text" id="time" name="time" readonly="readonly" /><!-- hidden or in another form --><span id="ampm"></span>