How To Retrieve Elapsed Time Since Windows Started

ID: Q113325


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, version 3.0


SUMMARY

The Windows API provides two functions (GetCurrentTime and GetTickCount) that return the number of milliseconds that have elapsed since Windows started. The GetTickCount function is identical in function to the GetCurrentTime function. However, GetTickCount should be used instead of GetCurrentTime because the name more closely matches its function.


MORE INFORMATION

GetTickCount returns the number of milliseconds since Windows started, and it resets to zero (0) if Windows is run continuously for approximately 49 days.

After the value has been retrieved, you can convert it to minutes and seconds and display it in the hh:mm:ss format.

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.


  2. Add a command button (Command1) to the form.


  3. Add the following to the General Declarations Section of Form1:
    
       Declare Function GetTickCount Lib "User" () As Long 


  4. Add the following code to the Command1_Click event:
    
       Sub Command1_Click ()
          Dim Count As Long
          Dim Days As Integer, Hours As Double
          Dim Minutes As Double, Seconds As Double
          Dim Message As String
    
          ' Get milliseconds since windows started
          Count = GetTickCount()
    
          ' Convert to Seconds
          Count = Count \ 1000
    
          ' Pull out Days
          Days = Count \ (24& * 3600&)
          If Days > 0 Then Count = Count - (24& * 3600& * Days)
    
          ' Pull out HH:MM:SS
          Hours = Count \ 3600&
          If Hours > 0 Then Count = Count - (3600& * Hours)
          Minutes = Count \ 60
          Seconds = Count Mod 60
    
          ' Create a Message with info in it
          Message = Days & " Day(s), " & Hours & " Hour(s), " & Minutes
          Message = Message & " Minute(s), " & Seconds
          Message = Message & " Second(s) Since Windows Started!"
    
          ' Display Message in a Message Box
          MsgBox Message
       End Sub 


  5. Start the program (or press the F5 key).


  6. Click the Command1 button. The message box should display the amount of time that has elapsed since Windows was started.



REFERENCES

For more information, see the Microsoft Windows SDK, Programmer's Reference (Book 2), Functions.

Additional query words: 2.00 3.00

Keywords :
Version :
Platform :
Issue type :


Last Reviewed: August 31, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.