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.
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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
Dates and times are often standardized on UTC time, which is analogous to GMT times. How to convert such times to your ...
Discover MoreNeed to check the current time in a formula you are putting together? It can sometimes be tricky to remember what Excel ...
Discover MoreIt is no secret that you can store time values in an Excel worksheet. But do you really know how small of a time value ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments