Title: Get Windows Running Time (From the Last Restart) using Delphi
The GetTickCount Windows API function retrieves the number of milliseconds that have elapsed since the system was started (up to 49.7 days).
If you need to display how many days (minutes, seconds) have passed since Windows have been started, you need to convert miliseconds to days, hours, minutes.
Here's a function to display Windows "up time":
function WindowsUpTime : string ;
function MSecToTime(mSec: Integer): string;
const
secondTicks = 1000;
minuteTicks = 1000 * 60;
hourTicks = 1000 * 60 * 60;
dayTicks = 1000 * 60 * 60 * 24;
var
D, H, M, S: string;
ZD, ZH, ZM, ZS: Integer;
begin
ZD := mSec div dayTicks;
Dec(mSec, ZD * dayTicks) ;
ZH := mSec div hourTicks;
Dec(mSec, ZH * hourTicks) ;
ZM := mSec div hourTicks;
Dec(mSec, ZM * minuteTicks) ;
ZS := mSec div secondTicks;
D := IntToStr(ZD) ;
H := IntToStr(ZH) ;
M := IntToStr(ZM) ;
S := IntToStr(ZS) ;
Result := D + '.' + H + ':' + M + ':' + S;
end;
begin
result := MSecToTime(GetTickCount) ;
end;
Note: the "WindowsUpTime" returns a string formatted as "days.hours:minutes:seconds", for example: "1.12:30:12", if Windows have been running for the last 1 day, 12 hours, 30 minutes and 12 seconds.