简体   繁体   English

如何在 Javascript-Jquery 的两个输入中从 HH:MM 时间总结小时和分钟?

[英]How can I sum hours and minutes from a HH:MM time in two inputs in Javascript-Jquery?

I have two inputs that come from two inputs with the format hh:mm.我有两个输入来自两个输入,格式为 hh:mm。

I would like to obtain the total number of hours spent.我想获得花费的总小时数。

GOOD
Example:
Input 1: 08:30
Input 2: 10:00
Total 1.5

In the following link you can see what I have tried to do, but the problem I am having is that it is not correctly subtracting the minutes at the time and instead of having the previous result I get this:在以下链接中,您可以看到我尝试做的事情,但我遇到的问题是它没有正确减去当时的分钟数,而不是获得以前的结果,我得到了这个:

WRONG
Example:
Input 1: 08:30
Input 2: 10:00
Total 2.5

http://jsfiddle.net/n9h5ztef/2/ http://jsfiddle.net/n9h5ztef/2/

____EDITED_____ I'm sorry I explained myself badly. ____EDITED_____ 对不起,我解释得不好。 I will try to put exactly what I have.我会尽量把我所拥有的。 What I am trying to do is a sum of all the hours there are, there are two morning and afternoon shifts.我想要做的是所有时间的总和,有两个上午和下午轮班。 I need to add all the hours.我需要添加所有的时间。

This is a real example:这是一个真实的例子:

1st shift
input1: 08:30
input2: 10:00

2nd shift
input3: 16:00
input4: 18:00

Total hours: 3.5

In the first turn he has done 1.5 hours and in the second two hours.在第一回合他已经完成了 1.5 个小时,在第二回合中他完成了 2 个小时。 Total 3.5.总计 3.5。

You can simply split on : change the minutes part to hours, and than calculate difference您可以简单地拆分:将分钟部分更改为小时,然后计算差异

 let getDifference = (time1, time2) => { let [h1, m1] = time1.split(':') let [h2, m2] = time2.split(':') return ((+h1 + (+m1 / 60)) - (+h2 + (+m2 / 60))) } console.log(getDifference("10:00", "08:30"))

Note:- Don't forgot to change string to numbers before doing any arithmetic operations, here + is implicitly converting string to number注意:-在进行任何算术运算之前不要忘记将字符串更改为数字,这里+是隐式将字符串转换为数字

Try this尝试这个

<html lang="en">
<body>
<input id="time1" value="08:30" size="5"> Time 1
<br>
<input id="time2" value="10:00" size="5"> Time 2
<br>
<button id="addTimes">Add times</button>
<br><br>Result <span id="timeSum">0</span>

<br>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
  const timeToNumber = (time) => {
    const parts = time.split(':');

    const part1 = parseInt(parts[0]);
    let part2 = parseInt(parts[1]);
    part2 = part2 / 60;

    return part1 + part2;
  };

  $("#addTimes").on('click', function () {
    const time1 = $('#time1').val();
    const time2 = $('#time2').val();

    const result = timeToNumber(time2) - timeToNumber(time1);
    $('#timeSum').text(result);
  });
</script>
</html>

I made this code sometime back to calculate the hours worked from an in time and out time precisely to the minute.我在某个时候制作了这段代码,以精确计算从进出时间到分钟的工作时间。 I modified it to work with two shifts.我修改了它以使用两班制。

 function myFunction() { var inTime2 = document.getElementById("in-time2").value; var outTime2 = document.getElementById("out-time2").value; var hoursIn2 = inTime2.split(':', 1); var hoursOut2 = outTime2.split(':', 1); var minutesArrayIn2 = inTime2.split(':', 2); var minutesArrayOut2 = outTime2.split(':', 2); var minutesIn2 = minutesArrayIn2[1]; var minutesOut2 = minutesArrayOut2[1]; var hoursToMinutesIn2 = hoursIn2 * 60; var hoursToMinutesOut2 = hoursOut2 * 60; var timeIn2 = hoursToMinutesIn2 + parseInt(minutesIn2); var timeOut2 = hoursToMinutesOut2 + parseInt(minutesOut2); var hoursWorked2 = (timeOut2 - timeIn2) / 60; var hoursInInt2 = parseInt(hoursIn2); var hoursOutInt2 = parseInt(hoursOut2); var minutesInInt2 = parseInt(minutesIn2); var minutesOutInt2 = parseInt(minutesOut2); var sumHours2; var sumMinutes2; if (minutesOut2 < minutesIn2) { var hoursOutInt3 = hoursOutInt2 - 1; var minutesOutInt3 = minutesOutInt2 + 60; sumHours2 = hoursOutInt3 - hoursInInt2; sumMinutes2 = minutesOutInt3 - minutesInInt2; document.getElementById("hours-worked2").innerHTML = sumHours2 + ":" + sumMinutes2; } else { sumHours2 = hoursOutInt2 - hoursInInt2; sumMinutes2 = minutesOutInt2 - minutesInInt2; document.getElementById("hours-worked2").innerHTML = sumHours2 + ":" + sumMinutes2; } var inTime = document.getElementById("in-time1").value; var outTime = document.getElementById("out-time1").value; var hoursIn = inTime.split(':', 1); var hoursOut = outTime.split(':', 1); var minutesArrayIn = inTime.split(':', 2); var minutesArrayOut = outTime.split(':', 2); var minutesIn = minutesArrayIn[1]; var minutesOut = minutesArrayOut[1]; var hoursToMinutesIn = hoursIn * 60; var hoursToMinutesOut = hoursOut * 60; var timeIn = hoursToMinutesIn + parseInt(minutesIn); var timeOut = hoursToMinutesOut + parseInt(minutesOut); var hoursWorked = (timeOut - timeIn) / 60; var hoursInInt = parseInt(hoursIn); var hoursOutInt = parseInt(hoursOut); var minutesInInt = parseInt(minutesIn); var minutesOutInt = parseInt(minutesOut); var sumHours; var sumMinutes; if (minutesOut < minutesIn) { var hoursOutInt1 = hoursOutInt - 1; var minutesOutInt1 = minutesOutInt + 60; sumHours = hoursOutInt1 - hoursInInt; sumMinutes = minutesOutInt1 - minutesInInt; document.getElementById("hours-worked").innerHTML = sumHours + ":" + sumMinutes; } else { sumHours = hoursOutInt - hoursInInt; sumMinutes = minutesOutInt - minutesInInt; document.getElementById("hours-worked").innerHTML = sumHours + ":" + sumMinutes; } var totalHoursTemp = sumHours + sumHours2; var totalMinutesTemp = sumMinutes + sumMinutes2; if (totalMinutesTemp >= 60) { totalHours = totalHoursTemp + 1; totalMinutes = totalMinutesTemp - 60; document.getElementById("total-hours").innerHTML = totalHours + ":" + totalMinutes; } else { document.getElementById("total-hours").innerHTML = totalHoursTemp + ":" + totalMinutesTemp; } }
 .table-custom2 { padding: 12px 13px; border-top: 1px solid #dddddd; text-align: center; } .table-custom3 { padding: 12px 13px; border-top: 1px solid #dddddd; text-align: center; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <th class="table-custom2">In Time</th> <th class="table-custom2">Out Time</th> <th class="table-custom2">Hours Worked</th> </tr> <tr> <td class="table-custom3"><input type="text" id="in-time1" class="input-xsmall" maxlength="5" oninput="myFunction()"></td> <td class="table-custom3"><input type="text" id="out-time1" class="input-xsmall" maxlength="5" oninput="myFunction()"></td> <td class="table-custom3"><span id="hours-worked"></span></td> </tr> <tr> <td class="table-custom3"><input type="text" id="in-time2" class="input-xsmall" maxlength="5" oninput="myFunction()"></td> <td class="table-custom3"><input type="text" id="out-time2" class="input-xsmall" maxlength="5" oninput="myFunction()"></td> <td class="table-custom3"><span id="hours-worked2"></span></td> </tr> </table> <p>Total Hours Worked : </p><span id="total-hours"></span>

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

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