Sum time in a period

Jordo82

New Member
Joined
Aug 31, 2004
Messages
47
I'm working on what is essentially a timesheet to help a friend calculate expected pay, but I've run into a roadblock in one area and would appreciate some help.

I have a date/time when she punches in and another when she punches out. Calculating total time spent on the job is easy, but I also need to know how many hours (if any) were spent working during a certain "golden time" period of the day. To make matters more complicated, this period is typically 11PM - 5AM, so the overnight issue complicates things.

For example, if she punches in at 7PM and punches out at 7AM the next day, she worked 12 hours total and 6 hours in the period 11AM - 5AM. If she works 12PM to 12AM, there's only 1 hour of "golden time". Essentially I want to sum the intersection of hours worked and the "golden time" period.

For optimum flexibility, I'd like to avoid hardcoding the definition of the "golden time" period. There's also the potential for 18 or 24 hour shifts so there may be multiple passes through the "golden time" period. I realize this may be more of a math problem than an Excel problem, but any help would be appreciated. Thanks!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Assuming you have a start time in A2 and an end time in B2 this formula will give you the total hours in a period defined in J2:J3 (earlier time must be in J2)

=(A2>B2)*MEDIAN(0,B2-J$2,J$3-J$2)+MAX(0,MIN(J$3,B2+(A2>B2))-MAX(J$2,A2))

so for your scenario you can put 05:00 in J2 and 23:00 in J3 and that formula will calculate the time worked within that period. Subtract that amount from the total hours and you'll get the time worked in 23:00 to 05:00.

If you want that all in one formula then you can use this version

=MOD(B2-A2,1)-((A2>B2)*MEDIAN(0,B2-J$2,J$3-J$2)+MAX(0,MIN(J$3,B2+(A2>B2))-MAX(J$2,A2)))

All result cells should be formatted as time. Works for shifts up to 24 hours
 
Upvote 0
Barry, brilliant! Thanks very much for your quick reply, I was really stumped with that one.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top