• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Time Difference Calculation Between Two Dates ...

uday

Member
Hi experts,

I have figured out one formula to calcute the time (hh:mm) difference between two dates. However it is not giving me the correct answer if the time span is beyond 2 days, it is only working within date or same date,.


Formula correction required:

=IF(B2 <= A2, "Out of Scope", (MIN(TIME(20,0,0), MOD(B2,1)) - MAX(TIME(8,0,0), MOD(A2,1))) + (INT(B2) - INT(A2) - 1) * (TIME(20,0,0) - TIME(8,0,0)) + IF(INT(B2) > INT(A2), MAX(0, TIME(20,0,0) - MAX(TIME(8,0,0), MOD(A2,1))) + MAX(0, MIN(TIME(20,0,0), MOD(B2,1)) - TIME(8,0,0)), 0 ))

A2B2Result
09/08/2024 08:00 AM11/08/2024 08:00 PM36:00:00
09/08/2024 07:00 AM11/08/2024 09:00 AM37:00:00
09/08/2024 07:00 PM11/08/2024 09:00 PM37:00:00
09/08/2024 10:00 AM11/08/2024 06:00 PM22:00:00
09/08/2024 08:00 PM12/08/2024 08:00 PM48:00:00

Result should be like above mentioned table, however it is not working, beyond 1 day or 2 days span. Sometime the result is not changing.
Also,if required then please suggest what could be the correct format to keep the result.
 
Formatting may help but...

So... Calculating date and time differences in Excel is both aided and hindered by the fundamental concept that when trying to look at hours, minutes & seconds only 24 hours are part of the logic. That's not to say that you can't increase the number by adding in more the way you have in your formula. However, it becomes increasingly simple if you handle each time segment independently.

I'm sure there are about 40 other ways to reach the outcome you're looking for but here is one approach that seems to fit the need:
Code:
=
IF(AND(INT(B2-A2)<=0, HOUR(B2-A2)<=0, MINUTE(B2-A2)<=0, SECOND(B2-A2)<=0), "Out of scope",
  IF((24*INT(B2-A2))  + HOUR(B2-A2)=0,"00", TEXT((24*INT(B2-A2))  + HOUR(B2-A2),"00"))
& ":"  &
  IF(MINUTE(B2-A2)=0,"00",TEXT(MINUTE(B2-A2),"00") )
& ":" &
  IF(SECOND(B2-A2)=0,"00",TEXT(SECOND(B2-A2),"00"))
)

Cheers!

 
Formatting may help but...
A lot it would seem:
1724686122756.png
and @vletm 's is a value you can do arithmetic with while @ExcelGoogler 's is text (although Excel's implicit data type conversion would probably come in if needed)! I've assumed UK style dates.

1724686865682.png

Edit post posting: Looking more closely at @uday 's formula I'm guessing it's trying to calculate the count of hours between 8am and 8pm for the date and times concerned? @uday , is that right and are weekends counted?
 
Last edited:
WOW! Nice, I like this much better but I would add to the condition just-in-case the difference is within the same day.
Code:
=IF(AND(INT(B2-A2)<=0, HOUR(B2-A2)<=0, MINUTE(B2-A2)<=0, SECOND(B2-A2)<=0),"Out of scope", B2-A2)

I haven't been very successful with selecting the correct formats over time so text is my go-to.

Also to @p45cal 's observation regarding 8am and 8pm, if that is the goal; are there any concerns for holidays?
 
Negative times are not challenge(, if someone would like to use it).
Excel can handle time if you turn on the 1904 date system,
but that will cause all dates to shift by 4 years and 1 day, so you'd have to correct that.
If you want that:
  • Select File > Options > Advanced.
  • Scroll down to the section 'When calculating this workbook'.
  • Tick the check box 'Use 1904 date system'.
  • Click OK.
Only uday can know, what uday tries to figure.
Last seen 14 Aug, 2024 (26 Aug, 2024)
 

ExcelGoogler

Why do You want to play with those hour, minute & second?
Below: B2 & B2 are same as well as C2 & C3.
This works always, if values are valid date & time.
If C2 < B2 then it works same way with C3 < B2 ... negative.
Screenshot 2024-08-27 at 07.46.30.png
... or there always can get absolute value and add "-" in front of result.
Screenshot 2024-08-27 at 08.05.18.png
 
vletm,
LOL Fair enough, I guess it all depends on your perspective... Microsoft Support Link
1724768448093.png
I haven't been very successful with selecting the correct formats over time so text is my go-to.
Why mess with perfection?
I'm sure there are about 40 other ways to reach the outcome you're looking for but here is one approach that seems to fit the need:
It's always been easier "for me" to prove to self-identified 'perfectionists' that needed their information to the second. I get what you're saying but from the user perspective, there are hidden arguments (format selections) that absolutely do change the information and in this situation, it changed the data for me.

I love that I learned something new that works! I may still use the separate "text" segments if I am trying to help others because it makes each part easier to see and understand.

Thank YOU so much for teaching me something new.

Cheers!
 
Back
Top