2

I've got a few day's worth of "clock in" and "clock out" entries in military (24 hour) time that are plain numbers.

Clock In | Clock Out
--------------------
1020     | 1555
1116     | 1857
1049     | 1204

I've manually figured out that this person has worked 14 hours and 31 minutes. I have an HTML page that contains a lot of these entries in a class, so I use the following code to get them in Javascript:

$('.clockin').each(function() {clockinsum += +$(this).text()||0;});
$('.clockout').each(function() {clockoutsum += +$(this).text()||0;});

I'm not sure where to go from here, or if this is even the right way to start. Is there a way for Javascript/jQuery to calculate the hours and minutes worked from a bunch of these entries?

Aaron
  • 531
  • 3
  • 8
  • 22
  • Do the values just show up as 4 digits or are they listed in a date/time value? Javascript can do date math for you if that's what you're asking, you just might need to convert those entries into a proper date/time object. – Jack Marchetti Mar 28 '17 at 14:48
  • @JackMarchetti In this case they are just 4 digits, but they don't need to be. What if these entries were in "00:00" instead? How would the calculation go? – Aaron Mar 28 '17 at 14:50
  • Pretty sure if you create those as Date() objects, you simply subtract them and you'll get the hours you're looking for. – Jack Marchetti Mar 28 '17 at 14:57
  • Possible duplicate of [Check time difference in Javascript](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – Heretic Monkey Mar 28 '17 at 16:20

2 Answers2

1

You need something to tell the timedifference.

In JavaScript you always work in milliseconds, so find the milliseconds difference between each start and end time, compound them and use that time to calculate time spent:

var list = [
  ["1020", "1555"],
  [1116, 1857],
  [1049, "1204"],
];
/**
 * Difference between two times in milliseconds
 *
 * @param {(number | string)} start
 * @param {(number | string)} end
 * @returns {number}
 */
function getTimeDifference(start, end) {
  var d1 = new Date(0);
  d1.setHours(parseInt(start.toString().substr(0, 2), 10));
  d1.setMinutes(parseInt(start.toString().substr(2, 2), 10));
  var d2 = new Date(0);
  d2.setHours(parseInt(end.toString().substr(0, 2), 10));
  d2.setMinutes(parseInt(end.toString().substr(2, 2), 10));
  return d2.getTime() - d1.getTime();
}
//figure how long this guy has worked:
var compiledTime = 0;
for (var index = 0; index < list.length; index++) {
  compiledTime += getTimeDifference(list[index][0], list[index][1]);
}
//At this point "compiledTime" contains the milliseconds the guy has worked
//Let's print it nice and pretty
var compiledTimeDate = new Date(compiledTime);
alert("Hours: " + compiledTimeDate.getHours() + "\n" +
  "Minutes: " + compiledTimeDate.getMinutes() + "\n" +
  "Seconds: " + compiledTimeDate.getSeconds() + "\n" +
  compiledTimeDate.getHours() + ':' + compiledTimeDate.getMinutes() + ':' + compiledTimeDate.getSeconds());
//From here you can use the Date object methods to do whatever
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28
1

If you already have the clock in and out in an array or can put them in an array, you could just do some simple math.

var clockIn = [1555, 1857, 1204];
var clockOut = [1020, 1116, 1049];
var timeWorked = 0;
//assuming for every clock in there is a clock out
for(var i = 0; i<=clockOut.length-1; i++){

   timeWorked = timeWorked+(clockIn[i] - clockOut[i]);

}
console.log(timeWorked);

This would return 1431, which translates to 14 hours and 31 min.

Koriley
  • 9
  • 2
  • You can't treat time as pure numbers, since they aren't `base 10`. Consider that the following: `1100-1009` would should return `0041` when working in time, but returns `91` in your example. – Emil S. Jørgensen Mar 29 '17 at 07:16
  • Emil, not to argue, but how do you figure? If I clocked in at 10:09 and clocked out at 11:00, that would mean I worked 91 minutes not 0041 minutes. Granted that would translate to an hour and fifty one minutes worked, but that would be easy logic to add to the code. I would love to better understand what you are saying, if I am wrong, I would rather learn why. Do you have a link I could read? – Koriley Mar 29 '17 at 13:38
  • If the times are just "what was on the clock" 60 minute hours, the difference between "nine past ten o'clock" and "eleven o'clock" translate to 41 minutes. That's the time difference. If however we consider the times as "minutes since midnight" then we can do as you do and simply subtract one number for another and get a 91 minutes difference. That's the numeric difference. From how i understand the question, OP wants the time differences worked, compound them and calculate how much time was worked overall. – Emil S. Jørgensen Mar 29 '17 at 13:53
  • I understand now Emil, thanks. That being said, I agree with the accepted answer more than mine. – Koriley Mar 29 '17 at 15:21