2

I want to format a ms value to a "minutes:seconds" value of two digits, by two digits I mean "00:00" but what I get is "0:0".

This is an example:

enter image description here

I want to show "02:04" instead "2:4"

This is my code:

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

UPDATE:

To solve this problem I did this, but I know exists a easy and improved way to do this:

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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417

3 Answers3

2

If you change you String.Format to String.Format("{0:mm\:ss}", Time) it will give you the result as 02:04 instead of 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:mm\:ss}", Time)
End Function
  • 1
    You don't need String.Format, you can just use `Time.ToString("mm\:ss")` which is a bit neater. – Chris Jul 03 '13 at 14:42
  • @Chris just perfect! Return New TimeSpan(TimeSpan.TicksPerMillisecond * MilliSeconds).ToString("mm\:ss"), thanks. – ElektroStudios Jul 03 '13 at 14:45
  • @ElektroHacker: You can go one better with `Return TimeSpan.FromMilliseconds(MilliSeconds).ToString("mm\:ss")` which removes that horrible looking `TicksPerMillisecond` use. :) – Chris Jul 03 '13 at 15:42
  • Thought I might as well add it as a new answer since it is actually different enough from this answer. :) – Chris Jul 03 '13 at 15:45
1

Use this Format String "{0:00}:{1:00}"

Andre
  • 1,228
  • 11
  • 24
1

You'll want to look at http://msdn.microsoft.com/en-us/library/ee372286.aspx and http://msdn.microsoft.com/en-us/library/ee372287.aspx for help on formats available.

That suggests you can use this as your whole method pretty much:

Return TimeSpan.FromMilliseconds(MilliSeconds).ToString("mm\:ss")

This is using the static FromMilliseconds method which takes a double but your Int64 will convert to that implicitly.

See http://msdn.microsoft.com/en-us/library/system.timespan.frommilliseconds.aspx for details on the method (and linked from there the rest of the class docs).

Chris
  • 27,210
  • 6
  • 71
  • 92