如何在if else语句中的输入type = text中使用值

Hisham Bawa

我创建了一个界面,供用户登录和注销时间并计算编号。工作时间。但是,我不完全了解我使用的代码,因此希望找到一个更容易理解的选项。我需要将文本的输入和输出时间转换为另一个代码,以检查员工是否工作了一整天(工作时间> 8小时)或半天等。

我用它来计算工作时间(用户将时间输入为hh:mm)

function diff(a, b) {
  function toTime(a) {
    return Date.parse('1970-01-01 ' + a.substr(0, 5)) / 1000 +
      (a.includes('PM') && (12 * 60));
  }
  var d = toTime(b) - toTime(a);
  return d >= 0 ? new Date(0, 0, 0, 0, 0, d).toTimeString().substr(0, 5) : '';
}

function myFunction() {

  var workhours;

  workhours = diff($('#in-time').val(), $('#out-time').val());
  $('#value').val(workhours);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <tr>
    <th>Employee Name</th>
    <th>In Time</th>
    <th>Out Time</th>
    <th>Hours Worked</th>
  </tr>

  <tr>
    <td>Test</td>
    <td><input type="text" id="in-time"></td>
    <td><input type="text" id="out-time" onkeyup="myFunction()"></td>
    <td><input type="text" id="value"></td>
  </tr>

</table>

编辑:如果有人可以根据两个输入来计算工作时间,那么如果有人可以解释,那就太好了,这样我就可以为以后的项目学习

Hisham Bawa

如果其他人正在尝试做类似的事情,我只是使这段代码起作用。我将在一段时间内更新它,并附上解释我如何工作的评论。

function myFunction() {
  
var inTime = document.getElementById("in-time").value; // assume its 08:30
var outTime = document.getElementById("out-time").value;// assume its 17:30

var hours = inTime.split(':',1); // array = [08]
var hours2 = outTime.split(':',1); // array = [17]

var minutesArray = inTime.split(':',2); // array = [08,30]
var minutesArray2 = outTime.split(':', 2); // array = [17,30]

var minutes = minutesArray[1]; // select '30' from the array [08,30]
var minutes2 = minutesArray2[1]; // select '30' from the array [17,30]

var hoursToMinutes = hours * 60;
var hoursToMinutes2 = hours2 * 60;

var time = hoursToMinutes + parseInt(minutes);
var time2 = hoursToMinutes2 + parseInt(minutes2);

var workhours =(time2 - time)/60;

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

<table>
  <tr>
    <th>Employee Name</th>
    <th>In Time</th>
    <th>Out Time</th>
    <th>Hours Worked</th>
  </tr>

  <tr>
    <td>Test</td>
    <td><input type="text" id="in-time"></td>
    <td><input type="text" id="out-time" onkeyup="myFunction()"></td>
    <td><span id="workhours"></span></td>
  </tr>

</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章