views:

176

answers:

4

Hi,

Say I convert some second into the TimeSpan object like this.

Dim sec= 1254234568
Dim t As TimeSpan = TimeSpan.FromSeconds(sec)

How to format TimeSpan object into a format like the following:

>105hr 56mn 47sec

Is there any built-in function? or I need to write a custom function.

Thanks.

+5  A: 

Well, the simplest thing to do is to format this yourself, e.g.

return string.Format("{0}hr {1}mn {2}sec",
                     (int) span.TotalHours,
                     span.Minutes,
                     span.Seconds);

In VB:

Public Shared Function FormatTimeSpan(span As TimeSpan) As String
    Return String.Format("{0}hr {1}mn {2}sec", _
                         CInt(Math.Truncate(span.TotalHours)), _
                         span.Minutes, _
                         span.Seconds)
End Function

I don't know whether any of the TimeSpan formatting in .NET 4 would make this simpler.

Jon Skeet
You may want to give VB code, though, as the code in the question looks like it :-)
Joey
Am I missing something? I get nervous when my answer disagrees with Jon Skeet :-)
Jason Williams
`((int) span.TotalMinutes) % 60` can be replaced by `span.Minutes`. The same thing with seconds.
lasseespeholt
Jason, your nervousness is well-justified.
John at CashCommons
Oops, yes, got carried away after the TotalHours - which *is* required.
Jon Skeet
HHow do I check if total hour > 1 then append "hrs" ortherwise, just append "hr" within the String.Format statement?
Maxd
@Maxd: Well, you could add an extra format parameter which ended up as "s" or "" depending on whether TotalHours was greater than 1. I don't have time to provide sample code right now I'm afraid.
Jon Skeet
@Joh Skeet I think, when you cast TotalHours to Integer, by default the TotalHours is rounded up. So, 34526 sec will be 10hr 35mn 26sec, which is not correct. 34526 sec = 9hr 35mn 26sec. Math.Floor(t.TotalHours) will do the job.
Angkor Wat
@Angkor: Wrong. Integral casts will truncate.
SLaks
@SLaks, I did try the sample he wrote. 34526 sec = 10hr 35mn 26sec, which is NOT correct. Did you try it?
Angkor Wat
@Angkor: Checking now...
Jon Skeet
@Angkor: Fixed, thanks. Blech - as far as I can tell there's no simple "convert and truncate" operator in VB, the equivalent of casting in C#.
Jon Skeet
@Angkor: Sorry; I was thinking in C#.
SLaks
+2  A: 

string.Format("{0}hr {1}mn {2}sec", (int) t.TotalHours, t.Minutes, t.Seconds);

Jason Williams
This is only for TimeSpan < 24 hr. And t,Seconds should be t.Seconds
Angkor Wat
You should change that to `{0:f0} ..., t.TotalHours, ...`
SLaks
@Angkor: Good point. Tweaked to cope with >24 hours
Jason Williams
Still, you need to tweak (int) t.TotalHours, see my comment on Jon Skeet post about rounding.
Angkor Wat
@Angkor Wat: ??? We don't want rounding, we want truncation.
Jason Williams
A: 

You may need to calculate the hours. The range for hours in TimeSpan.ToString is only 0-23.

The worst you'll need is to do raw string formatting a la Jon Skeet.

John at CashCommons
A: 

Try this Function:

Public Shared Function GetTimeSpanString(ByVal ts As TimeSpan) As String
        Dim output As New StringBuilder()

        Dim needsComma As Boolean = False

        If ts = Nothing Then

            Return "00:00:00"

        End If

        If ts.TotalHours >= 1 Then
            output.AppendFormat("{0} hr", Math.Truncate(ts.TotalHours))
            If ts.TotalHours > 1 Then
                output.Append("s")
            End If
            needsComma = True
        End If

        If ts.Minutes > 0 Then
            If needsComma Then
                output.Append(", ")
            End If
            output.AppendFormat("{0} m", ts.Minutes)
            'If ts.Minutes > 1 Then
            '    output.Append("s")
            'End If
            needsComma = True
        End If

        Return output.ToString()

 End Function       

Convert A Timespan To Hours And Minutes

Angkor Wat