0

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);
Neo
  • 2,305
  • 4
  • 36
  • 70
  • 2
    [Standard TimeSpan Format Strings](http://msdn.microsoft.com/en-us/library/ee372286.aspx) & [Custom TimeSpan Format Strings](http://msdn.microsoft.com/en-us/library/ee372287.aspx) – dtb Feb 07 '12 at 16:58

2 Answers2

4

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.

Community
  • 1
  • 1
rie819
  • 1,249
  • 12
  • 19
  • My issue is formatting it within the report which keeps coming back as an error! I have in the class a timespan created using the code I just added to the question but when I show it on the report it comes back with #Error – Neo Feb 08 '12 at 09:46
2

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".

Ross
  • 46,186
  • 39
  • 120
  • 173