How can I format the output of the Stopwatch? I tried the following:
but VS complains. I would like to show just the minutes & seconds (to 3 decimal places) of the elapsed time. Thanks...Code:sw.Elapsed.ToString("mm:ss")
Printable View
How can I format the output of the Stopwatch? I tried the following:
but VS complains. I would like to show just the minutes & seconds (to 3 decimal places) of the elapsed time. Thanks...Code:sw.Elapsed.ToString("mm:ss")
The timespan does not provide an overrideable ToString method, so you are going to have to manually build the string:
Code:MessageBox.Show(sw.Elapsed.Minutes.ToString("00") & ":" & sw.Elapsed.Seconds.ToString("00") & "." & sw.Elapsed.Milliseconds)
Notice that I used TotalMinutes and not Minutes. If you use Minutes then you'll never go past 59, so a value of an hour or more would show the incorrect time. Notice that I also forced the milliseconds to three figures, other wise 1 millisecond would be displaye as "00:00.1" instead of "00:00.001".vb.net Code:
Dim elapsed As TimeSpan = myStopwatch.Elapsed MessageBox.Show(String.Format("{0:00}:{1:00}.{2:000}", _ elapsed.TotalMinutes, _ elapsed.Seconds, _ elapsed.Milliseconds))
Nice catch.Quote:
Originally Posted by jmcilhinney
Thanks guys...
OK, I found a problem with using TotalMinutes. It rounds to the nearest whole minute, so an elapsed time of 31 seconds gets displayed as 01:31, whereas using Minutes displays 00:31, as it should be. Here is a little snippet that will demonstrate this:Quote:
Originally Posted by jmcilhinney
Code:Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Show()
Dim sw As New Stopwatch
sw.Start()
System.Threading.Thread.Sleep(31000) 'sleep for 31 seconds
sw.Stop()
MessageBox.Show(String.Format("{0:00}:{1:00}.{2:000}", sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds) & ControlChars.CrLf & String.Format("{0:00}:{1:00}.{2:000}", sw.Elapsed.TotalMinutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds))
End Sub
Ah, sorry about that. I have used code like this before and I left a bit out. You do use TotalMinutes, not Minutes, but to account for the issue you mention you have to trucate the decimal part first. Change my code to this:The Math.Floor method will remove the decimal part of the value, thus ensuring it will never be rounded up.vb.net Code:
Dim elapsed As TimeSpan = myStopwatch.Elapsed MessageBox.Show(String.Format("{0:00}:{1:00}.{2:000}", _ Math.Floor(elapsed.TotalMinutes), _ elapsed.Seconds, _ elapsed.Milliseconds))
Can anyone explain please the quote code above works fine, but my code...
... returns an error - System.FormatException: 'Input string was not in a correct format.' - on running please?Code:ts = myStopWatch.Elapsed
elapsedTime = ts.ToString(String.Format("{0:00}:{1:00}:{2:000}", Math.Floor(ts.TotalMinutes), ts.Seconds, ts.Milliseconds))
Label8.Text = elapsedTime
EDIT:
I didn't need the ts.ToString on line 2. All good now!Code:ts = myStopWatch.Elapsed
elapsedTime = String.Format("{0:00}:{1:00}:{2:000}", Math.Floor(ts.TotalMinutes), ts.Seconds, ts.Milliseconds)
Label8.Text = elapsedTime