简体   繁体   English

在文本框模糊事件中以 24 小时格式格式化时间

[英]Format time in 24 hour format in textbox blur event

i have created two text box which will take time as input from user i want when user press tab key the value of text box converted to 24 hour format i have written a method我已经创建了两个文本框,当用户按下 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;
}

It is working fine for following format它适用于以下格式

1.) 0 - 00:00 2.) 9.3 - 09:30 3.) 13.3 - 13:30 4.) 9 - 09:00 1.) 0 - 00:00 2.) 9.3 - 09:30 3.) 13.3 - 13:30 4.) 9 - 09:00

but when i enter但是当我进入

1352 it should give me 13:52 but it is giving 13:00 where i did wrong any help? 1352 它应该给我 13:52 但它给了 13:00 我做错了什么帮助?

you are setting mins to the string '00' in that case.在这种情况下,您将 mins 设置为字符串 '00'。 So you are comparing a string to a number multiple times in the second line.因此,您在第二行中多次将字符串与数字进行比较。 Eventually setting mins to itself, '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 : 

What happens when pos is -1?pos为 -1 时会发生什么? Well, when pos is -1, you're left with:好吧,当pos为 -1 时,您将得到:

var mins = "00";
//...
result = hour + ":" + mins;

How does pos end up being -1? pos怎么会变成-1? Well, any time that time doesn't contain a colon or period:好吧,任何时候该time都不包含冒号或句点:

var pos = time.indexOf(":");
if (pos < 0) {
    pos = time.indexOf(".");
}

In particular, mins will be "00" when time is 1352. You need an else branch on your final if to handle the case when there is no colon or period.特别是,当time为 1352 时, mins将为"00" 。如果没有冒号或句点,则需要在 final if上添加一个else分支来处理这种情况。

How about怎么样

DEMO HERE在这里演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM