How to convert this timespan

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?WVhR?=

    #1

    How to convert this timespan

    Hello,
    I want to convert the timespan (1.12:25:23) to 1day 12hour 25min 23sec, how
    to do it? thank you.
  • Martin H.

    #2
    Re: How to convert this timespan

    Hello YXQ,

    That format you provided is not a timespan, but seems to be a custom
    string format. Therefore, the information has to be "cut" out of the
    string. If it was just a TimeSpan value (in a timespan variable), then
    you could just use the "Days", "Hours", "Minutes" and "Seconds"
    properties of the timespan variable to get your result.

    To deal with a string in the specified format, you could use this method:

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As _
    System.EventArg s) Handles Button1.Click
    MsgBox(Convert( "1.12:25:23 "))
    End Sub

    Private Function Convert(ByVal TimeSpanString As String) As String
    Dim retVal As String
    Dim posi As Integer, t As Integer
    Dim iTSS As String, iDays As String, iSplit() As String

    iTSS = Trim(TimeSpanSt ring)
    posi = InStr(TimeSpanS tring, ".")

    If posi 0 Then
    iDays = Strings.Left(iT SS, posi - 1)
    iTSS = Strings.Right(i TSS, iTSS.Length - posi)
    Else
    iDays = 0
    End If

    iSplit = Split(iTSS, ":")

    retVal = iDays & "day "

    For t = 0 To UBound(iSplit)

    If t 2 Then
    Exit For 'Just in case the string is longer
    End If

    retVal &= iSplit(t)
    Select Case t
    Case 0
    retVal &= "hour "

    Case 1
    retVal &= "min "

    Case 2
    retVal &= "sec"
    End Select
    Next

    Return retVal
    End Function

    Best regards,

    Martin
    On 27.09.2008 10:01, YXQ wrote:
    Hello,
    I want to convert the timespan (1.12:25:23) to 1day 12hour 25min 23sec, how
    to do it? thank you.

    Comment

    • Jay B. Harlow [MVP - Outlook]

      #3
      Re: How to convert this timespan

      Have you tried TimeSpan.Parse?

      Dim s As String = "1.12:25:23 "
      Dim time As TimeSpan = TimeSpan.Parse( s)

      Alternatively you can use TimeSpan.TryPar se.

      Dim s As String = "1.12:25:23 "
      Dim time As TimeSpan
      If TimeSpan.TryPar se(s, time) Then
      ' Its a valid time span
      End If


      --
      Hope this helps
      Jay B. Harlow
      ..NET Application Architect, Enthusiast, & Evangelist
      T.S. Bradley - http://www.tsbradley.net


      "YXQ" <YXQ@discussion s.microsoft.com wrote in message
      news:B5945481-FFCB-463B-BB06-AF25402BAD7C@mi crosoft.com...
      Hello,
      I want to convert the timespan (1.12:25:23) to 1day 12hour 25min 23sec,
      how
      to do it? thank you.

      Comment

      Working...