我想将 ms 值格式化为两位数的“分钟:秒”值,两位数的意思是“00:00”,但我得到的是“0:0”。
这是一个例子:
我想显示“02:04”而不是“2:4”
这是我的代码:
Label_TrackPosition.Text = Format_Time(sender.value)
Private Function Format_Time(ByVal MilliSeconds As Int64) As String
Dim Time As New TimeSpan(TimeSpan.TicksPerMillisecond * MilliSeconds)
Return String.Format("{0}:{1}", Time.Minutes, Time.Seconds)
End Function
更新:
为了解决这个问题,我这样做了,但我知道存在一种简单且改进的方法来做到这一点:
Private Function Format_Time(ByVal MilliSeconds As Int64) As String
Dim Time As New TimeSpan(TimeSpan.TicksPerMillisecond * MilliSeconds)
Select Case Time.Minutes.ToString.Length
Case 1
Select Case Time.Seconds.ToString.Length
Case 1
Return String.Format("0{0}:0{1}", Time.Minutes, Time.Seconds)
Case 2
Return String.Format("0{0}:{1}", Time.Minutes, Time.Seconds)
End Select
Case 2
Select Case Time.Seconds.ToString.Length
Case 1
Return String.Format("{0}:0{1}", Time.Minutes, Time.Seconds)
Case 2
Return String.Format("{0}:{1}", Time.Minutes, Time.Seconds)
End Select
End Select
' Return String.Format("{0}:{1}", Time.Minutes, Time.Seconds)
End Function