Friday 1 November 2019

worksheet function - Excel Elapsed Time as Plain Numbers Subtraction Formula


For sake of entry simplicity and to prevent Excel from inserting unnecessary time of day hours and AM/PM information, I am using this custom format to enter time in minutes and seconds: "00\:00"


I am trying to calculate time difference with this type of data, but Excel doesn't recognize it as base 60 numbering. Is there a way I can keep this format and still calculate a result?


e.g. If A1 reads "00:30" and B1 reads "01:21", how do I get C1 to calculate and result in "00:51"?



Answer



Here's a way to do it with a formula, although there are so many parentheses in this equation I'm about to go cross-eyed.


enter image description here


Since the rightmost two digits in your custom format represent the seconds, we can separate them using RIGHT(). Then subtracting that from the original number gives the "minutes" in your custom format.


I'll start out with the pieces, and then build the formula from its parts.


Convert B to Seconds  =  (60*(B2-RIGHT(B2,2))/100)+RIGHT(B2,2)
Convert A to Seconds = (60*(A2-RIGHT(A2,2))/100)+RIGHT(A2,2)
Difference in Seconds = ((60*(B2-RIGHT(B2,2))/100)+RIGHT(B2,2))-((60*(A2-RIGHT(A2,2))/100)+RIGHT(A2,2))

Now to convert back to the original format:
1. Divide the difference by 60, truncate it, and multiply by 100 to get the minutes part.
2. Divide the difference by 60, and take the modulus to get the seconds part.
3. Add 1 and 2.

Minutes Part = 100*(TRUNC((((60*(B2-RIGHT(B2,2))/100)+RIGHT(B2,2))-((60*(A2-RIGHT(A2,2))/100)
Seconds Part = MOD((((60*(B2-RIGHT(B2,2))/100)+RIGHT(B2,2))-((60*(A2-RIGHT(A2,2))/100)+RIGHT(A2,2))),60)

And here's the entire formula:

=100*(TRUNC((((60*(B2-RIGHT(B2,2))/100)+RIGHT(B2,2))-((60*(A2-RIGHT(A2,2))/100)+RIGHT(A2,2)))/60))+MOD((((60*(B2-RIGHT(B2,2))/100)+RIGHT(B2,2))-((60*(A2-RIGHT(A2,2))/100)+RIGHT(A2,2))),60)

Whew! Hope this helps.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...