Forum Discussion

Mahammad_Asim's avatar
Mahammad_Asim
Copper Contributor
Dec 23, 2023
Solved

I need a Custom Date And Time Calculating formula

I want to calculate the total working hours of an employee who will start working on 23/12/2023 at 08:00 AM and end on 24/12/2023 at 05:30 PM. However, Our working hours are split into two shifts...
  • SnowMan55's avatar
    SnowMan55
    Dec 28, 2023

    Mahammad_Asim 

    What is the problem?  As you will see in the attached workbook, I re-created the range data that you showed, created the shifts table very similar to what flexyourdata showed (though I put it on a different worksheet, and the #N/A values are not required), and entered their formula (adjusting it to reference the correct cells, plus some cleanup) into column H.  It produces the correct number of hours (but not an Excel time value) for Excel 365 for Windows, and I expect it to work for the web version, also.


    The flexyourdata formulas create an internal array (named mins) for each minute between the Start date-time and Stop date-time, and then use a derivative of that array (without dates) to do lookups into the shifts table to create another array (named xl) of the shift identifiers (some possibly blank) for an identical time-of-day or (more likely) the latest time-of-day entry that is earlier.  (The end times in the table are just documentation; they are not used.) Unstated assumptions:

    • The work shift identifiers will be numbers.
    • You don't need a time precision smaller than a minute.


    That technique is flexible.  However, it is performing many lookups for each formula recalculation. That may cause opening the workbook (or otherwise causing these formulas to recalculate) to be slow as you get many rows of data.  Faster alternatives are possible.


    Maintenance note: In the future, if the work shifts are changed, you should either replace Work Time formulas for already-used rows with their values (as calculated under the older shift times), or set up a separate Excel table for the new shift times, and use that new table in formulas going forward.

     

Resources