How Can I calculate the average of a given List which content is described below:
06:56
06:58
06:55
06:54
06:54
06:53
06:55
06:53
06:58
06:54
06:58
06:55
06:54
06:50
06:54
06:57
Any idea ?!?
How Can I calculate the average of a given List which content is described below:
06:56
06:58
06:55
06:54
06:54
06:53
06:55
06:53
06:58
06:54
06:58
06:55
06:54
06:50
06:54
06:57
Any idea ?!?
var times = new List<string>
{
"06:56",
"06:58",
"06:55",
"06:54",
"06:54",
"06:53",
"06:55",
"06:53",
"06:58",
"06:54",
"06:58",
"06:55",
"06:54",
"06:50",
"06:54",
"06:57"
};
var average = times
.Select(TimeSpan.Parse)
.Average(x => x.TotalMilliseconds);
var averageTime = TimeSpan.FromMilliseconds(average);
Convert the values to TimeSpan. The you can average directly with the output as the correct data type.
Addendum
Adding to the answer from @gabba providing a list of strings and the link above using ticks, you can calculate the average this way:
TimeSpan average = new TimeSpan(Convert.ToInt64(times.Average(x => TimeSpan.Parse(x).Ticks)));
The result value is, of course, the same: 06:54:52.500