Darkrym Darkrym - 2 years ago 206
Vb.net Question

VB convert MM:SS timecode to seconds

I am trying to use time span and total seconds but it can't convert string to long. This is the code I have so far, I am trying to convert a

timestamp
in the format of HH:MM:SS and MM:SS (stored in a
listbox
) to total seconds then set the play head / current position (of window media player) to the total seconds of the time stamp. the error i am getting is Conversion from string to type 'Long' is not valid.

Stamp = "01:22:22"

Dim SetPos As New TimeSpan(Stamp)'error happens just here (Conversion from string to type 'Long' is not valid.)
Dim Seconds As Double = SetPos.TotalSeconds


Dim controls As WMPLib.IWMPControls3 = plrVideo.Ctlcontrols
controls.currentPosition = Seconds 'setting play head to seconds from start


Sorry for the terrible code I've only been coding for a little bit.

Answer Source

I know its way over complicated but this is what i ended up with thanks to D le

Dim timeString = Stamp.Split(":")

    If UBound(timeString) = 2 Then

        Dim hour = timeString(0)
        Dim min = timeString(1)
        Dim sec = timeString(2)

        Dim totalSecs As Integer = CInt(hour) * 60 * 60 + CInt(min) * 60 + CInt(sec)

        SetPos = totalSecs
    Else
        If UBound(timeString) = 1 Then
            Dim min = timeString(0)
            Dim sec = timeString(1)

            Dim totalSecs As Integer = CInt(min) * 60 + CInt(sec)

            SetPos = totalSecs
        End If
    End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download