Get the time Windows has been running for
By: Lorenzo Dalla Vecchia Date: 24/06/2000

Windows includes the GetTickCount API function that returns the number of milliseconds (ms) since Windows startup. Using that function, you can find out the time Windows has been running for.

As always, we need a API declaration; put the following in the Declarations section of a module:

Public Declare Function GetTickCount Lib "kernel32" _
Alias
"GetTickCount" () As Long

Now we just need to convert the number of milliseconds returned by GetTickCount in a more readable format (days, hours, minutes, seconds). Keep in mind the following:

1 second = 1,000 ms
1 minute = 60,000 ms
1 hour = 3,600,000 ms
1 day = 86,400,000 ms

Here's the code:

Dim d As Integer
Dim h As Integer
Dim m As Integer
Dim s As Integer
Dim TimeStamp As String

'Get milliseconds from API

iTick = GetTickCount

'Convert milliseconds in days, hrs, mins, secs

d = iTick \ 86400000
h = (iTick Mod 86400000) \ 3600000
m = ((iTick Mod 86400000) Mod 3600000) \ 60000
s = (((iTick Mod 86400000) Mod 3600000) Mod 60000) \ 1000

'Create a "time stamp" string

TimeStamp = Format(d, "0000 days ") & _
Format(h, "00 hours ") & _
Format(m, "00 minutes ") & _
Format(s, "00 seconds ")

The four central lines actually convert milliseconds in days, hours, minutes, seconds. The first line performs the "integer division" by 86,400,000 to get days; the remainder is divided by 3,600,000 to get hours, the new remainder is divided by 60,000 to get minutes and the last remainder is divided by 1,000 to get seconds.
In the last block of lines, the four values are concatenated in a more readable
TimeStamp string, like:

0003 days, 17 hours, 03 minutes, 27 seconds

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it