Last post Oct 25, 2013 10:01 PM by markfitzme

Member

65 Points

326 Posts

### How can we calculate the Average time.

Hi,

I am counting values from database like:

Total calls Made In Seconds:1995 ,Total Calls Received In Seconds :20214 , Total Call Duration:1:4:54

The above values are storing In Seconds( In database).Here I am converting that to HH:MM:SS(Below is the Function):

```public string ConvertToTimeFormat(int seconds)
{
string time = "";
string hours = "";
string mins = "";
string sec = "";
if (seconds == 0)
{
time = "0";
}
else
{
if (seconds >= 3600)
{
hours = (seconds / 3600).ToString();
mins = ((seconds % 3600) / 60).ToString();
sec = ((seconds % 3600) % 60).ToString();
}
else if (seconds < 3600)
{
hours = "0";
mins = (seconds / 60).ToString();
sec = (seconds % 60).ToString();
}
time = hours + ":" + mins + ":" + sec;
}
return time;
}```

So I am getting Below Output :

here how to calculate Average Time Duration:

(1995 +20214) / (43+5 )

How can we calculate the Average time(In hrs:mm:ss)

Thanks

skg

All-Star

25721 Points

5781 Posts

### Re: How can we calculate the Average time.

Your ConvertToTimeFormat function can be drastically shortened to:

```public string ConvertToTimeFormat(int seconds)
{
TimeSpan ts = new TimeSpan(0,0,seconds);
return ts.ToString(@"hh\:mm\:ss");
}```

You should be able to calculate the average time by passing your formula straight to your function. Not knowing what your variables are,