javascript - Calculating time difference with three time values - Stack Overflow

I'm having trouble calculating a time (hours:minutes) difference between three time values present

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'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  
Share Improve this question edited Jan 9, 2010 at 1:35 th3hamburgler asked Jan 9, 2010 at 1:00 th3hamburglerth3hamburgler 6443 gold badges9 silver badges24 bronze badges 1
  • please give us some context code. – Hogan Commented Jan 9, 2010 at 1:06
Add a ment  | 

5 Answers 5

Reset to default 5

I'd suggest thinking about this slightly differently...given your three times, convert all times into pure hours, then do the math as you would in hours. If you need, you can turn the fractional hours back into minutes.

Any time can be turned into hours just by dividing the number of minutes by 60 and adding the result to the hours portion. Your math then gets very easy, it's the same expression you have except you don't have to do time based math.

If you're given an input of something of the form "hh:mm" where hh and mm are integers then you can pute pure hours using the following expression:

var mytime = "hh:mm";
var temp = mytime.split(":");
var hours = temp[0] + (temp[1]/60);

edit: Others have noted problems occurring if you deal with a night shift. A simple solution (not requiring creation of Date objects) is to add 24 hours to the start time when the end time is less than the start time (and that of course can be done in the "pure hours" domain).

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 bination.

In that case, lunchDuration === (endLunch - startLunch), so you can calculate timeAtWork as follows:

timeAtWork = (endWork - startWork) - lunchDuration

Hope this helps!

I am not enough of a JavaScript expert to help you there but since you seem to be using relitive times, if your place works 24 hours, pay special attention to any shift that crosses midnight. 7:00 Thursday - 23:00 Wednesday = 8.

Use the getTime() function, which returns number of milliseconds since Jan 1, 1970. So your code would look like:

var time_worked_ms = time_out.getTime() - lunch.getTime() - time_in.getTime();

time_worked_ms would contain the number of milliseconds for your interval.

Given 2 "HH:mm" strings, the following method will return a float number of the time between them:

function timeDiff(time1, time2) {
    var time1array = time1.split(":");
    var time2array = time2.split(":");
    var hour1 = parseInt(time1array[0]);
    var min1 = parseInt(time1array[1]);
    var hour2 = parseInt(time2array[0]);
    var min2 = parseInt(time2array[1]);
    return (hour1 + min1/60) - (hour2 + min2/60);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744661842a4586505.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信