Here is an easier/non-VBA method as ashleedawg said would probably exist: (I will use A1
as the Start Date, and B1
as the End Date)
The "tricky" part here is that each shift skips over midnight - to correct for this, I will subtract the Start Time (Time(20,0,0)
) from all datetimes to zero-out.
Step 1
Start with the "full days" count - so, "23rd to 25th" is "2 days" - and then multiply that by the "hours-per-day": (The MOD
is to automatically correct for whether or not the shift crosses midnight)
=(Int(B1-Time(20,0,0))-Int(A1-Time(20,0,0)))*Mod(1+Time(5,0,0)-Time(20,0,0),1)
Step 2
Next, we need to trim any pre-start time from the first day. Since we are aligning the start-time to "00:00" anyway, we can just MAX
that against 0: (To get just the time, we use MOD(A1,1)
):
=-MAX(0, MOD(A1,1)-TIME(20,0,0))
Step 3
Last, we need to trim any post-end time from the last day. We will use MIN
to take the earlier time as cut-off this time:
=+MIN(Mod(1+Time(5,0,0)-Time(20,0,0),1), MOD(B1-TIME(20,0,0),1))
Add it up
Put all of that into one formula, and there you go:
=(INT(B1-TIME(20,0,0))-INT(A1-TIME(20,0,0)))*MOD(1+TIME(5,0,0)-TIME(20,0,0),1)-MAX(0, MOD(A1,1)-TIME(20,0,0))+MIN(MOD(1+TIME(5,0,0)-TIME(20,0,0),1), MOD(B1-TIME(20,0,0),1))
{EDIT} You can change TIME(20,0,0)
and TIME(5,0,0)
to cell references that contain your shift start/end times. The code should work so long as the shifts are under 24-hours
[hh]:mm:ss
? – user4039065