I’m having trouble calculating a time (hours:minutes) difference between three time values presented as HH:mm strings.
I’m calculating time worked in a day from time in, lunch time and time out.
e.g.
var time_worked = time_out – lunch – time_in
or 07:00 = 17:00 – 01:00 – 09:00
I’m familiar with the php dates but the javascript date object is scary!
Code:
var time_in = $(".time-in").val(); // 09:00
var time_out = $(".time-out").val(); // 17:00
var lunch = $(".lunch").val(); // 01:00
var time = time_out - lunch - time_in; // should be 07:00 or 7
I think you’re misconceptualizing your problem domain.
Specifically, if you’re trying to find
timeAtWork, and everything is represented as a time object (e.g. in seconds since epoch), there are FOUR important times here you need to know (the three you listed [startWork, endWork and startLunch] and the one you missed, [endLunch]). In that case, you can calculate your desired value by the following subtractions, assuming that whatever language you’re working in supports adding/subtracting dates (for JavaScript, look at the excellent date.js library)timeAtWork = (endWork-endLunch) + (startLunch-startWork)or equivelantly
timeAtWork = (endWork - startWork) - (endLunch - startLunch)However, it sounds to me like you have two ‘time’ variables [startWork and endWork] and one ‘timespan’ variable [lunchDuration]. In this case, lunchDuration is something like 3600 (one hour), but does not represent a true date/time combination.
In that case,
lunchDuration === (endLunch - startLunch), so you can calculate timeAtWork as follows:timeAtWork = (endWork - startWork) - lunchDurationHope this helps!