Reputation: 2395
I have a decimal value (64.17 hours) and need to convert this to a timestamp of hours, minutes and seconds but the hours value should show 64 not 16.
It should also not show the microseconds so 64.17 would be displayed as
64:10:12
Anyone help me on this? I also need to put it in an aggregate function (average in this case)
TimeSpan ts = new TimeSpan();
ts.Add(TimeSpan.FromHours(ncr_time_lost));
Console.WriteLine(ncr_time_lost + " = " + TimeSpan.FromHours(ncr_time_lost).ToString());
return TimeSpan.FromHours(ncr_time_lost);
Upvotes: 0
Views: 1476
Reputation: 18106
Update Working Code Add the below to the code part for the report
Public Function TimeSpanToString(source as Timespan) as String
Dim result As String
result = Convert.ToInt32(source.TotalHours).ToString() & ":" & source.Minutes.ToString() & ":" & source.Seconds
return result
End Function
Here is the answer with an example: TimeSpan.FromHours Method.
Update: The format "64:10:12" can be done with the following code:
static string TimeSpanToString(TimeSpan source)
{
string result = string.Format("{0}:{1}:{2}",
(int)source.TotalHours,
source.Minutes,
source.Seconds);
return result;
}
Update: Don't know whether it's correct, but try the following to use it from RDLC:
public class SomeClass
{
public TimeSpan Time { get; set; }
public string FormattedTime
{
get { return TimeSpanToString(Time); }
}
private static string TimeSpanToString(TimeSpan source)
{
// Code.
}
}
Then try to get it from the report as "=Fields!FormattedDate.Value"
.
Upvotes: 2
Reputation: 1249
Why not just use the TimeSpan structure? Yes it keeps track of days, but there's also a TotalHours property that should give you everything your looking for.
You can also do any averaging calculations on the TotalMilliseconds property.
See here for more information on formatting the TimeSpan.
Upvotes: 4