如何从2个字段中获取总分钟数

时间:2019-02-07 22:52:56

标签: javascript jquery

我想获取结果的总分钟数,

字段1 14:30
场2 14:45
结果00:15

Example

我正在使用以下代码,但是,我只想获得24小时。 14:46:22 GMT-0800(太平洋标准时间)不好 14:46:22我想要这个结果,同时也要

$(
  function() {
    let lastFocusEl = null;

    $('#time').click(function() {
      var time = new Date();
      if (lastFocusEl) lastFocusEl.value = time.toTimeString();
    });

    let f = function() {
      lastFocusEl = this
    };
    $('#time-holder').focus(f);
    $('#time-holder2').focus(f);

  }
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" value="" id="time-holder">
<input type="text" value="" id="time-holder2">
<input type="text" value="" id="result">

<input type="button" value="time" name="timer" id="time">

1 个答案:

答案 0 :(得分:0)

您可以从日期中仅提取想要的部分作为输入值。

$(()=>{
  let lastFocusEl = null;
  
  $('#time').on('click', e => {
    if (!lastFocusEl) return;

    let time = new Date();
      
    // pull off just the parts you want into the value
    lastFocusEl.value = `${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}`;
  });
  
  $('#time-holder, #time-holder2').on('focus', e => lastFocusEl = e.target);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input type="text" value="" id="time-holder">
<input type="text" value="" id="time-holder2">
<input type="text" value="" id="result">

<input type="button" value="time" name="timer" id="time">