Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Calculating Elapsed Time with Excluded Periods.

# Calculating Elapsed Time with Excluded Periods

by Allen Wyatt
(last updated February 11, 2017)

Mahesh can figure out how to calculate the difference between two dates and times in minutes. However, he would like to calculate the difference in minutes, yet exclude the hours between 5:00 pm and 8:00 am as well as excluding everything between 5:00 pm Friday to 8:00 am Monday. For example, if the first date is 02/18/09 6:00 pm and the end date is 02/19/09 9:00 am, the correct result should be 60 minutes. Mahesh wonders if this possible to do with a formula.

As should be obvious, a formula to achieve the desired result could be very complex. Many subscribers provided different solutions, including some great user-defined functions. Rather than focus on all of them, I figured I would just jump right to the most elegant (shortest) formula and suggest using it.

Assume that your starting date/time was in cell A1 and the ending date/time was in cell B1. Given these you could use the following formula:

```=(NETWORKDAYS(A1,B1)-1)*("17:00"-"08:00")
+IF(NETWORKDAYS(B1,B1),MEDIAN(MOD(B1,1),"17:00"
,"08:00"),"17:00")-MEDIAN(NETWORKDAYS(A1,A1)
*MOD(A1,1),"17:00","08:00")
```

This is a single formula; it returns an elapsed time. This means that you will need to format the cell to show elapsed time. If you prefer to have the result as a regular integer, then you should use this version of the formula, instead:

```=((NETWORKDAYS(A1,B1)-1)*("17:00"-"08:00")
+IF(NETWORKDAYS(B1,B1),MEDIAN(MOD(B1,1),"17:00"
,"08:00"),"17:00")-MEDIAN(NETWORKDAYS(A1,A1)
*MOD(A1,1),"17:00","08:00"))*1440
```

The change (multiplying the original result by 1440) results in a number of minutes rather than an elapsed time. The value 1440 is derived by multiplying 60 by 24 to get the number of minutes in a day.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12004) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Calculating Elapsed Time with Excluded Periods.

##### Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

##### MORE FROM ALLEN

Printing a Chart

It is inevitable that if you spend time creating a chart you will want to print that chart on your printer. Here's how ...

Discover More

Adding labels to a chart can make the information presented in the chart more understandable. Excel allows you to add ...

Discover More

Putting the Last Saved Date in a Cell

Do you need a cell in your worksheet to display the date on which the workbook was last saved? This can be a bit tricky, ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

##### More ExcelTips (ribbon)

Automatically Entering a Data Entry Time

Excel worksheets can be used to keep track of all sorts of information. You may want to use it, for instance, to track ...

Discover More

Entering or Importing Times without Colons

Enter a time into a cell and you normally include a colon between the hours and minutes. If you want to skip that pesky ...

Discover More

Converting Numeric Values to Times

If you have a bunch of times entered into cells without the colon between the hours and minutes, chances are good that ...

Discover More
##### Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. Youâ€™ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 - 8?

2017-11-29 18:27:48

Larry Edgar-Smith

Allen - Thanks for this! I have been wrestling with this calculation for a while, and this helped me to get it done! The only thing I added was the Holidays parameter from a list I had.

2017-04-12 16:51:54

Stone Giant

FrankT: NETWORKDAYS(B1,B1) can return 0 or 1 depending on whether or not the cell contains a weekend, holiday, or business day. Your formula makes an invalid assumption that A1 and B1 always contains a business day.

2017-02-14 10:11:08

FrankT

NETWORKDAYS(B1,B1)= 1
NETWORKDAYS(A1,A1)= 1
therefore just:

=(NETWORKDAYS(A1,B1)-1)*("17:00"-"08:00")
+MEDIAN(MOD(B1,1),"17:00","08:00")
-MEDIAN(MOD(A1,1),"17:00","08:00")

2016-07-31 15:08:53

maesh

can any one help me to covert same formula in SQL server function or procedure I tried to convert but not getting correct result.

2015-08-30 03:23:47

Robert

I REALLY like this tip, i have a report that requires me to exclude non-working hours for IT fault cases and this formula does that perfectly! Thanks

2015-08-28 09:55:25

Yvan Loranger

ooops, would help if I format properly.
Original 2 formulae are good

2014-11-23 14:22:21

Yvan Loranger

You have trouble because formula is wrong. Try
=IF(NETWORKDAYS(A1,B1)<2,0,
(NETWORKDAYS(A1,B1)-2)*9) +
IF(INT(A1)=INT(B1),MEDIAN(MOD(B1,1)*24,8 ,17)-MEDIAN(MOD(A1,1)*24,8,17),
17-MEDIAN(MOD(A1,1)*24,8,17)+MEDIAN(MOD(B1,1)*24,8,17)-8)
Take example from Thursday to Tuesday, NETWORKDAYS gives 4,subtract 2 for Thursday & Tuesday which we'll deal differently since they may not be full 9 hr days. IF 1st day=last day then use last time - 1st time
ELSE [for Thurs & Tues] 17-1st time (Thurs) + last time(Tues)-8

2013-07-30 12:33:27

Aby

I as trying to decipher this formula but couldn't.Could you please explain a bit.

##### This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.