Take date/time and calculate the time in between excluding weekends and including partial days

freeb1893

Board Regular
Joined
Jul 30, 2012
Messages
234
Office Version
  1. 365
Platform
  1. Windows
I have 2 dates with dates and time, where I'd like to subtract them and determine the amount of time between those dates. I'd like to exclude weekends in the result. I'd also like to consider only partial days if the time for either the 1st date or the 2nd date is within the day sometime.

Anyone know how to put this in a formula?

Also if possible, I'd like to be able to define what my full day is, based on a start time and end time, and factor that into consideration.

For example if in cells G1 and H1 for references, I define the start of a date as 8:00AM and end of that same date as 4:00PM respectively as what constitutes 1 full day in my result.

In cells A1 and B1 I have 4/28/2023 12:00PM and 5/2/2023 4:00PM respectively.

I'd like the result of my formula referencing those cells to be 2.50 days.
 
In G1, it contains the specified start time, and H1 contains the specified end time. The main formula is divided into 3 parts:

Part 1: Full end date - full start date
=NETWORKDAYS(A2, B2)

Part 2: Subtract hours greater than 8:00 from the result of Part 1
-(MAX(MOD(A2,1),$G$1)-$G$1)/8*24

Part 3: Subtract hours less than 16:00 from the result of Part 2
-($H$1-MIN($H$1,MOD(B2,1)))/8*24

The final formula: Part 1 - Part 2 - Part 3
=ROUND(NETWORKDAYS(A2, B2)-(MAX(MOD(A2,1),$G$1)-$G$1)/8*24-($H$1-MIN($H$1,MOD(B2,1)))/8*24,6)

Note: In practice, if the start time is earlier than 8:00 (e.g., 7:00) or the end time is later than 16:00 (e.g., 20:00), it is still considered to start at 8:00 and end at 16:00.
(In my sample it is yellow range)

Book1
ABCGH
1StartEndHour08:0016:00
228-04-23 Fri 12:0002-05-23 Tue 16:002.50
316-05-23 Tue 08:0016-05-23 Tue 16:001.00
416-05-23 Tue 07:0017-05-23 Wed 18:002.00
516-05-23 Tue 08:0018-05-23 Thu 16:003.00
616-05-23 Tue 08:0019-05-23 Fri 16:004.00
716-05-23 Tue 08:0020-05-23 Sat 16:004.00
816-05-23 Tue 08:0021-05-23 Sun 16:004.00
916-05-23 Tue 08:0022-05-23 Mon 16:005.00
1008-05-23 Mon 08:3010-05-23 Wed 08:061.95
1116-05-23 Tue 08:0024-05-23 Wed 16:007.00
Sheet1
Cell Formulas
RangeFormula
C2:C11C2=ROUND(NETWORKDAYS(A2,B2)-(MAX(MOD(A2,1),$G$1)-$G$1)/8*24-($H$1-MIN($H$1,MOD(B2,1)))/8*24,6)
 
Upvote 0
Solution

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
In G1, it contains the specified start time, and H1 contains the specified end time. The main formula is divided into 3 parts:

Part 1: Full end date - full start date
=NETWORKDAYS(A2, B2)

Part 2: Subtract hours greater than 8:00 from the result of Part 1
-(MAX(MOD(A2,1),$G$1)-$G$1)/8*24

Part 3: Subtract hours less than 16:00 from the result of Part 2
-($H$1-MIN($H$1,MOD(B2,1)))/8*24

The final formula: Part 1 - Part 2 - Part 3
=ROUND(NETWORKDAYS(A2, B2)-(MAX(MOD(A2,1),$G$1)-$G$1)/8*24-($H$1-MIN($H$1,MOD(B2,1)))/8*24,6)

Note: In practice, if the start time is earlier than 8:00 (e.g., 7:00) or the end time is later than 16:00 (e.g., 20:00), it is still considered to start at 8:00 and end at 16:00.
(In my sample it is yellow range)

Book1
ABCGH
1StartEndHour08:0016:00
228-04-23 Fri 12:0002-05-23 Tue 16:002.50
316-05-23 Tue 08:0016-05-23 Tue 16:001.00
416-05-23 Tue 07:0017-05-23 Wed 18:002.00
516-05-23 Tue 08:0018-05-23 Thu 16:003.00
616-05-23 Tue 08:0019-05-23 Fri 16:004.00
716-05-23 Tue 08:0020-05-23 Sat 16:004.00
816-05-23 Tue 08:0021-05-23 Sun 16:004.00
916-05-23 Tue 08:0022-05-23 Mon 16:005.00
1008-05-23 Mon 08:3010-05-23 Wed 08:061.95
1116-05-23 Tue 08:0024-05-23 Wed 16:007.00
Sheet1
Cell Formulas
RangeFormula
C2:C11C2=ROUND(NETWORKDAYS(A2,B2)-(MAX(MOD(A2,1),$G$1)-$G$1)/8*24-($H$1-MIN($H$1,MOD(B2,1)))/8*24,6)
Thank you! Now if I wanted to change the start and end time range where basically I look at all 24 hours of the day as constituting 1 day rather than 8 hours, how would I adjust this formula to do that? I've played around with this, but can't seem to figure out how to tweak this to do that
 
Upvote 0
So, you want to count hours with no "off" time so 1 day is 24 hours?
For instance, do you want the number of hours from 10 AM on day 1 to 4:00 PM on day 3? Are you concerned about weekends or holidays?
You may want to start a new thread with the specific question, as it seems this thread was a slightly different question.
 
Upvote 0
So, you want to count hours with no "off" time so 1 day is 24 hours?
For instance, do you want the number of hours from 10 AM on day 1 to 4:00 PM on day 3? Are you concerned about weekends or holidays?
You may want to start a new thread with the specific question, as it seems this thread was a slightly different question.
Will do! And then maybe a more related question, out of curiosity how hard is it to adjust the formula to look at maybe a 10 hour day rather than an 8 hour day? I've been playing around with the formulas changing 8's to 10's, but seems like there's more too it than that to get it work with a 10 hour timeframe rather than 8 hours?
 
Upvote 0
what are your new start and end times? Which suggested solution are you trying to alter, you haven't marked the correct solution.
 
Upvote 0
what are your new start and end times? Which suggested solution are you trying to alter, you haven't marked the correct solution.
So these are the test start and end times I'm working with, with 8:00AM through 4:00PM being my "8 hour full work day":

Start Date / TimeEnd Date / TimeTotal Hours / 8 = Days
10/4/2023 9:00​
10/4/2023 8:00​
0​
10/4/2023 7:00​
10/4/2023 15:00​
0.875​
10/4/2023 7:00​
10/4/2023 18:00​
1​
10/3/2023 10:00​
10/4/2023 14:00​
1.5​
10/3/2023 6:00​
10/4/2023 14:00​
1.75​
10/1/2023 0:00​
10/30/2023 12:00​
20.5​
10/4/2023 0:00​
10/4/2023 23:59​
1​
9/25/2023 14:38​
9/26/2023 13:31​
0.860417​
5/10/2023 0:00​
5/10/2023 16:00​
1​

So I've actually made modifications to your formula, as well as bebo's formula, where both yours and bebo's produce the same results. I added some additional logic to handle some additional conditions, like if my start date and end dates are blank, or if the end date/time is earlier than than my start date/time, etc. My start time of my 8 hour timeframe is in I12 and I23 respectively below, and end time is in I13 and I24

=IF(ROUND(NETWORKDAYS(A13,B13)-(MAX(MOD(A13,1),$I$12)-$I$12)/8*24-($I$13-MIN($I$13,MOD(B13,1)))/8*24,6)<0,0,ROUND(NETWORKDAYS(A13,B13)-(MAX(MOD(A13,1),$I$12)-$I$12)/8*24-($I$13-MIN($I$13,MOD(B13,1)))/8*24,6))

=(IF(NETWORKDAYS.INTL($A24,$A24,"0000011")=0,0,IF(24*(IF(AND(INT($B24)=INT($A24),$B24-INT($B24)<TIME($I$24,0,0)),$B24-INT($B24),TIME($I$24,0,0))-IF($A24-INT($A24)<TIME($I$23,0,0),TIME($I$23,0,0),$A24-INT($A24)))<0,0,24*(IF(AND(INT($B24)=INT($A24),$B24-INT($B24)<TIME($I$24,0,0)),$B24-INT($B24),TIME($I$24,0,0))-IF($A24-INT($A24)<TIME($I$23,0,0),TIME($I$23,0,0),$A24-INT($A24)))))+IF(INT($B24)-INT($A24)<=1,0,$I$23*NETWORKDAYS.INTL($A24+1,$B24-1,"0000011"))+IF(NETWORKDAYS.INTL($B24,$B24,"0000011")=0,0,IF(INT($B24)-INT($A24)=0,0,IF(24*((IF($A24-INT($B24)>TIME($I$24,0,0),TIME($I$24,0,0),$B24-INT($B24)))-TIME($I$23,0,0))<0,0,IF(24*((IF($A24-INT($B24)>TIME($I$24,0,0),TIME($I$24,0,0),$B24-INT($B24)))-TIME($I$23,0,0))>$I$23,24*((IF($B24-INT($A24)>TIME($I$24,0,0),TIME($I$24,0,0),$A24-INT($A24)))-TIME($I$23,0,0)),24*((IF($A24-INT($B24)>TIME($I$24,0,0),TIME($I$24,0,0),$B24-INT($B24)))-TIME($I$23,0,0)))))))/$I$23
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

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