How can I code to see how long the computer has been on.
Simple examples of code if possible.
How can I code to see how long the computer has been on.
Simple examples of code if possible.
You use GetTickCount function see this example.
program Ticks;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils;
function TicksToStr(Ticks: Cardinal): string; //Convert Ticks to String
var
aDatetime : TDateTime;
begin
aDatetime := Ticks / SecsPerDay / MSecsPerSec;
Result := Format('%d days, %s', [Trunc(aDatetime), FormatDateTime('hh:nn:ss.z', Frac(aDatetime))]) ;
end;
begin
try
Writeln('Time Windows was started '+ TicksToStr(GetTickCount));
Readln;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.
UPDATE
to get the info in other format just must edit this line,
Result := Format('%d days, %d hours %d minutes %d seconds ', [Trunc(aDatetime), HourOf(aDatetime),MinuteOf(aDatetime),SecondOf(aDatetime) ]) ;
and add the unit DateUtils.
Note that GetTickCount isn't really designed for accuracy.
For more reliable timing, use the QueryPerformanceCounter and QueryPerformanceFrequency api calls:
function SysUpTime : TDateTime;
var
Count, Freq : int64;
begin
QueryPerformanceCounter(count);
QueryPerformanceFrequency(Freq);
if (count<> 0) and (Freq <> 0) then
begin
Count := Count div Freq;
Result := Count / SecsPerDay;
end
else
Result := 0;
end;