How To Retrieve Elapsed Time Since Windows Started

Last reviewed: June 21, 1995
Article ID: Q113325
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic 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 reference words: 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.