|
-
Oct 23rd, 2007, 02:23 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Format output of Stopwatch
How can I format the output of the Stopwatch? I tried the following:
Code:
sw.Elapsed.ToString("mm:ss")
but VS complains. I would like to show just the minutes & seconds (to 3 decimal places) of the elapsed time. Thanks...
-
Oct 23rd, 2007, 02:44 PM
#2
Re: [2005] Format output of Stopwatch
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)
-
Oct 23rd, 2007, 10:19 PM
#3
Re: [2005] Format output of Stopwatch
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))
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".
-
Oct 24th, 2007, 06:58 AM
#4
Re: [2005] Format output of Stopwatch
 Originally Posted by jmcilhinney
Notice that I used TotalMinutes and not Minutes.
Nice catch.
-
Oct 24th, 2007, 10:22 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Format output of Stopwatch
-
Oct 24th, 2007, 12:51 PM
#6
Thread Starter
Frenzied Member
Re: [2005] Format output of Stopwatch
 Originally Posted by jmcilhinney
Notice that I used TotalMinutes and not Minutes.
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:
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
-
Oct 24th, 2007, 06:34 PM
#7
Re: [RESOLVED] [2005] Format output of Stopwatch
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:
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))
The Math.Floor method will remove the decimal part of the value, thus ensuring it will never be rounded up.
-
Sep 11th, 2025, 12:56 AM
#8
Junior Member
Re: [RESOLVED] [2005] Format output of Stopwatch
 Originally Posted by jmcilhinney
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:
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))
The Math.Floor method will remove the decimal part of the value, thus ensuring it will never be rounded up.
Can anyone explain please the quote code above works fine, but my code...
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
... returns an error - System.FormatException: 'Input string was not in a correct format.' - on running please?
EDIT:
Code:
ts = myStopWatch.Elapsed
elapsedTime = String.Format("{0:00}:{1:00}:{2:000}", Math.Floor(ts.TotalMinutes), ts.Seconds, ts.Milliseconds)
Label8.Text = elapsedTime
I didn't need the ts.ToString on line 2. All good now!
Last edited by MHutcheon; Sep 11th, 2025 at 01:02 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|