I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an agent talks about 5 hours per day, this comes out to about 150 hours of talk time per month. Fine.
But, the DateTime and TimeSpan format strings will only output hours from 0-23. I need something that will output time as such:
150:0:0
to represent 150 hours, 0 minutes, and 0 seconds.
Is there any way to do this? I'm binding these values to a DataGrid, so it needs to be outputted in the above manner and not in the "6.6:0:0" that the TimeSpan ToString() method outputs.
I created a custom format provider by implementing the ICustomFormatter and ICustomProvider interfaces, but this is not my preferred solution. There must be some way to do this, alternatively, that I am overlooking? Thanks for your assistance.
Regards,
Jeffrey Shantz MCP
P.S. (The reason that using a CustomerFormatter is not my preferred solution is this:
I need a summary row at the bottom of the datagrid calculating the totals of all the times listed in the datagrid rows. I keep a running total by incrementing in the ItemDataBound() event and then display that running total in the footer (the summary row). All fine, except that the ItemDataBound() event fires *after* each row is added, meaning that I have to do something like this: mRunningTotal += TimeSpan.Parse(e.Item.Cells[0].Text). The problem is that if the cell text is formatted using my custom formatter, it will be outputted something like this: "150:0:0". But the TimeSpan.Parse() method only allows for 0-23 hours, so it throws an Exception.
This may not make much sense to you outside the scope of my project, but this explanation was just provided for a little background info)
I think that TimeSpan is enought to handle your expectations.
TimeSpan have a "Days" property that you can simply recalculate
into hours e.g.:
int totalHours=5*timeSpan.Days + timeSpan.Hours;
There's also "TotalHours" property... but it seems to
count day as 24 hours.
Use custom string formating by "String.Format" to get
wanted format e.g.:
string formatedTimeSpan=String.Format("{0}:{1:00}:{2:00}"
, totalHours
, timeSpan.Minutes
, timeSpan.Seconds);
There will be more problems with Parse(...), but you
can always use the constructor.
Regards
Marcin
> Hello,
>
> I'm developing a large statistics application for a call center. It often needs to calculate time spanning over months. For example, an agent's total talk time within 30 days. Since an agent talks about 5 hours per day, this comes out to about 150 hours of talk time per month. Fine.
>
> But, the DateTime and TimeSpan format strings will only output hours from 0-23. I need something that will output time as such:
>
> 150:0:0
>
> to represent 150 hours, 0 minutes, and 0 seconds.
>
> Is there any way to do this? I'm binding these values to a DataGrid, so it needs to be outputted in the above manner and not in the "6.6:0:0" that the TimeSpan ToString() method outputs.
>
This should do it:
TimeSpan span = /* however you calculate the Timespan */;
string s = String.Format( "{0}:{1}:{2}",
(int) span.TotalHours,
span.Minutes,
span.Seconds);
If you fit this logic into whatever mechanism you need to generate the
formatted output, you should be set.
--
mikeb