How to Calculate Working Hours Between 2 Dates [Solution]

Share

Facebook
Twitter
LinkedIn

This post builds on earlier discussion, How many hours did Johnny work? I recommend you to read that post too.

Lets say you have 2 dates (with time) in cells A1 and A2 indicating starting and ending timestamps of an activity. And you want to calculate how many workings hours the task took. Further, lets assume,

  • Start date is in A1 and End date is in A2
  • Work day starts at 9 AM and ends at 6PM
  • and weekends are holidays

Now, if you were to calculate total number of working hours between 2 given dates, the first step would be to understand the problem thru, lets say a diagram like this:

Working hours between 2 dates - how to write a formula

We would write a formula like this:

=(18/24-MOD(A1,1)+MOD(A2,1)-9/24)*24 + (NETWORKDAYS(A1,A2)-2)*9

See the above illustration to understand this formula.

Now, while this formula is not terribly long or ineffective, it does feel complicated.

May be we can solve the problem in a different way?!?

Michael left an interesting answer to my initial question, how many hours did Johnny work?

Pedro took the formula further with his comment.

The approach behind their formulas is simple and truly out of box.

Instead of calculating how many hours are worked, we try to calculate how many hours are not worked and then subtract this from the total working hours. Simple!

See this illustration:

Working hours between 2 dates - a better formula

So the formula becomes:

Total working hours between 2 dates – (hours not worked on starting day + hours not worked on ending day)

=NETWORKDAYS(A1,A2)*9 - (MOD(A1,1)-9/24 + 18/24 -MOD(A2,1))*24

After simplification, the formula becomes,

=NETWORKDAYS(A1,A2)*9 - (MOD(A1,1) -MOD(A2,1))*24 -9

=(NETWORKDAYS(A1,A2)-1)*9 +(MOD(A2,1)-MOD(A1,1))*24

Sixseven also posted an equally elegant formula that uses TIME function instead of MOD()

=(NETWORKDAYS(B3,C3)*9) - ((TIME(HOUR(B3),MINUTE(B3),SECOND(B3))-TIME(9,0,0))*24) - ((TIME(18,0,0)-TIME(HOUR(C3),MINUTE(C3),SECOND(C3)))*24)

Download the solution Workbook and play with it

Click here to download the solution workbook and use it to understand the formulas better.

Thanks to Pedro & Michael & Sixseven & All of you

If someone asks me what is the most valuable part of this site, I would proudly say, “the comments”. Every day, we get tens of insightful comments from around the world teaching us various important techniques, tricks and ideas.

Case in point: the comments by Michael, Pedro and Sixseven on the “how many hours…” post taught me how to think out of box to solve a tricky problem like this with an elegant, simple formula. Thank you very much Michael, Pedro, Sixseven and each and every one of you who comment. 🙂

Have a great weekend everyone.

PS: This weekend is my mom’s birthday, plus it is a minor festival in India. So I am going to eat sumptuously, party vigorously and relax carelessly. Next week is going to be big with launch of excel school 3.

PPS: While at it, you may want to sign up for excel school already. The free lesson offer will vanish on Wednesday.

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

11 Responses to “Who is the most consistent seller? [BYOD]”

  1. Hui... says:

    The Date column in the sample file is Text not Dates

  2. Great Chandoo. Keep it up, Looking forward more from BYOD..

  3. gayani says:

    Thanks

  4. Frank Tonsen says:

    With Excel 2013 the pivot table could be connected to the data model which provides a distinct count.

  5. Mak says:

    This will do for invoice count
    =COUNTIF(F:F,H12)
    Instead of
    =COUNTIFS(sales[SELLER],$H12)

  6. Alejandro says:

    Excellent document. How did you make the last graphic? Witch app. Thanks for answer.

  7. Chris says:

    Can someone tell me what =countif(sales[date],sales[date]) is counting? The value is 19. Its found in the =SUMPRODUCT(IF(sales[SELLER]=H12,1/COUNTIFS(sales[SELLER],H12,sales[date],sales[date]),0))

    • Vândalo says:

      Hi Chris,

      =countif(sales [date],sales[date]) function is counting the unique dates in the table.

      Vândalo

  8. Nguyen says:

    Excellent document!

    Can you explain more about the calculation on Weighted consistency? More specific the small number is 0,00001 ?

    How come the number should be smaller if there is more sellers?

  9. TS says:

    Hi,

    Not understood this formula: {=SUMPRODUCT(IF(sales[SELLER]=H12,1/COUNTIFS(sales[SELLER],H12,sales[date],sales[date]),0))}

    Please explain.

    Thanks.

Leave a Reply