繁体   English   中英

从输入的时间类型中获取小时和分钟

[英]getting hours and minutes from input type of time

关于如何使用 JavaScript 从这种格式“hh:mm”中单独获取小时和分钟的任何建议?

<input type="time" id="timepicker" onchange="timePassed(this.value)">

<script>
    function timePassed(time) {
        var hours;
        var mins;
        //todo: get the hours and minutes from the input value and store them in separate variables
    }
</script>

我尝试使用 getHours() 和 getMinutes() 但它似乎不适用于 hh:mm 格式

 function timePassed(time) { let[hours, mins] = time.split(":"); console.log(hours); console.log(mins); //todo: get the hours and minutes from the input value and store them in separate variables }
 <input type="time" id="timepicker" onchange="timePassed(this.value)">

如果您希望您的时间以 24 小时为增量显示,那么上面提到的建议就可以了。 如果你想让它分成 12 小时的增量,那么试试这个。

<input type="time" id="timepicker" onchange="timePassed(this.value)">

<script>
  function timePassed(time) {                
    let [hours, mins] = time.split(":");
    hours = hours/12 > 1 ? hours-12 : hours;
    console.log(hours);
    console.log(mins);
  }
</script>

暂无
暂无

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

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