我正在尝试使用时间跨度和总秒数,但它无法将字符串转换为长整数。这是我到目前为止的代码,我试图将HH:MM:SS和MM:SS(存储在timestamp
)格式的listbox
转换为总秒数然后设置播放头部/当前位置(窗口媒体播放器)到时间戳的总秒数。我得到的错误是从字符串转换为类型' Long'无效。
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
对于可怕的代码我很抱歉,我只编写了一些代码。
答案 0 :(得分:0)
我猜你会得到一个' HH:MM:SS'类似的字符串
lblDebug.Text = Stamp
如果我的猜测是正确的,你可以做一个
Dim timeString = lblDebug.Text.Split(":")
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)
答案 1 :(得分:0)
我知道它的方式过于复杂,但这是我最后得到的感谢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
答案 2 :(得分:0)
以下函数将返回字符串的秒数:
Function GetSeconds(Stamp As String) As Long
Try
'Try the format "hh:mm:ss", when failed then try "mm:ss"
Return TimeSpan.ParseExact(Stamp, {"hh\:mm\:ss", "mm\:ss"}, System.Globalization.CultureInfo.InvariantCulture).TotalSeconds
Catch ex As Exception
'Invalid Format!
Return -1
End Try
End Function
输出示例:
Debug.WriteLine(GetSeconds("30:03")) '--> 1803
Debug.WriteLine(GetSeconds("01:22:22")) '--> 4942
Debug.WriteLine(GetSeconds("01:23:45:67")) '--> -1 (Error)
答案 3 :(得分:0)
如果要以“[[HH:] MM:] SS”格式解析时间字符串,可以使用以下简短代码段:
Function ParseTime( timeAsString As String) As TimeSpan
Dim secondsAsLong As Long
For Each partAsString In timeAsString.Split(":")
Dim partAsLong As Long
Long.TryParse(partAsString, partAsLong)
secondsAsLong *= 60
secondsAsLong += partAsLong
Next
Dim timeAsTimeSpan = TimeSpan.FromSeconds(secondsAsLong)
Return timeAsTimeSpan
End Function
这使您可以解析时间字符串的所有变体,只要它们是
空输入也会产生值为零的有效TimeSpan
。
事实上,:
之间的所有无效部分都将被视为蜜蜂0