Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. 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

Written by Allen Wyatt (last updated August 19, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


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, 2016, 2019, Excel in Microsoft 365, and 2021. 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

Moving the Insertion Point in a Macro

One of the common things done in macros is to somehow "process" documents, which often means moving the insertion point ...

Discover More

Positioning a Column on the Screen

If you have static columns and dynamic columns on the screen, you may want the dynamic columns to always show a ...

Discover More

Determining How Many Windows are Open

You can open multiple documents at the same time in Word, and each document occupies its own document window. Here's a ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

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

Using Excel for Timing

Excel allows you to store times in a worksheet. If you want to use Excel to time certain events, there are a couple of ...

Discover More

Calculating an Average Time

When working with elapsed times, you may want to calculate an average of those times. This tip demonstrates just how easy ...

Discover More
Subscribe

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

View most recent newsletter.

Comments

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}] (all 7 characters, in the sequence shown) 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 two less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


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.

Newest Tips
Subscribe

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

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.