Home > Enterprise >  How to subtract fix hours in an Excel formula?
How to subtract fix hours in an Excel formula?

Time:12-05

I've been looking on the internet and here on the site, but I don't find an example: I would like to calculate the difference between two hours, but if one is in the morning (smaller than 12:00) and the other in the afternoon (larger than 13:00), an hour should be subtracted.

As the values "08:00", "16:00" and "17:00" are accepted by Excel, I thought this would be easy, something like:

=IF(AND(B3<=12:00,C3>=13:00),C3-B3-1,C3-B3)

But the values "12:00" and "13:00" are not recognised by Excel. How can I write my formula (as readable as possible, please, calculating 13/24 and using this constant in my formula is not very readable :-) ).

CodePudding user response:

I've opted for this solution:

=IF(AND(B3<=TIME(12,0,0),C3>=TIME(13,0,0)),C3-B3-TIME(1,0,0),C3-B3)

This says precisely what I want: in case I started in the morning (B3<=TIME(12,0,0)) and ended in the afternoon (C3>=TIME(13,0,0)), then I subtract one hour TIME(1,0,0) from the difference (taking lunchtime into account).

  • Related