Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleMeasure elapsed time in VB .NET
KeywordsVB.NET, elapsed time, time, timer function, seconds
CategoriesSoftware Engineering, Tips and Tricks
 
Declare two DateTime variables for the start and stop times. Set their values using the Now function when the operation starts and stops.

Use the stop time variables's Subtract method to subtract the start time and obtain a result of type TimeSpan. Use the TimeSpan's TotalSeconds method to get the total number of seconds as a Double. (The Seconds method returns the whole number of seconds in the TimeSpan.) Use the resulting Double's ToString method to turn the result into a string.

 
Private Sub btnStart_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnStart.Click
    Static start_time As DateTime
    Static stop_time As DateTime
    Dim elapsed_time As TimeSpan

    If btnStart.Text = "Start" Then
        lblElapsed.Text = ""
        start_time = Now
        btnStart.Text = "Stop"
    Else
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)
        lblElapsed.Text = _
            elapsed_time.TotalSeconds.ToString("0.000000")
        btnStart.Text = "Start"
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated