I wrote a function which does this functionality and I am not satisfied with the performance & implementation. I wanted to see if someone can help me implement this nifty functionality in an efficient way.
The function should return a string which is the average time between 2 timer values passed. 2 timer values are both strings of the format "MM:SS:HS" for example if values "02:02:20" and "04:04:40" are passed to the function it should output the average time between those values and return it as a string in the same format "03.03.30".
I wanted to implement it in C#, Java is fine too.
Look forward to your solutions. Thanks for reading!
TimeSpan.Parseto parse the two valuesTimeSpan.Ticks) and divide it by two to get the average (as you can’t divide aTimeSpandirectly)TimeSpan.FromTicksto convert back to aTimeSpanToStringto convert back from aTimeSpanto a string.(I’m not sure why the other answers are suggesting subtraction, when a mean value is usually gained by adding values and then dividing…)
Personally I’d probably split this into two methods, one of which just did the averaging part, and the other of which did the text conversions:
You may need to mess around a bit to get the exact text format you want – there are more options in .NET 4 than previously, using
ToStringwith a format string etc.