Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Format output of Stopwatch

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [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...

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Format output of Stopwatch

    vb.net Code:
    1. Dim elapsed As TimeSpan = myStopwatch.Elapsed
    2.  
    3. MessageBox.Show(String.Format("{0:00}:{1:00}.{2:000}", _
    4.                               elapsed.TotalMinutes, _
    5.                               elapsed.Seconds, _
    6.                               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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2005] Format output of Stopwatch

    Quote Originally Posted by jmcilhinney
    Notice that I used TotalMinutes and not Minutes.
    Nice catch.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: [2005] Format output of Stopwatch

    Thanks guys...

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: [2005] Format output of Stopwatch

    Quote 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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim elapsed As TimeSpan = myStopwatch.Elapsed
    2.  
    3. MessageBox.Show(String.Format("{0:00}:{1:00}.{2:000}", _
    4.                               Math.Floor(elapsed.TotalMinutes), _
    5.                               elapsed.Seconds, _
    6.                               elapsed.Milliseconds))
    The Math.Floor method will remove the decimal part of the value, thus ensuring it will never be rounded up.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Junior Member
    Join Date
    Oct 2022
    Location
    West Sussex
    Posts
    29

    Re: [RESOLVED] [2005] Format output of Stopwatch

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. Dim elapsed As TimeSpan = myStopwatch.Elapsed
    2.  
    3. MessageBox.Show(String.Format("{0:00}:{1:00}.{2:000}", _
    4.                               Math.Floor(elapsed.TotalMinutes), _
    5.                               elapsed.Seconds, _
    6.                               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
  •  



Click Here to Expand Forum to Full Width