Mega Code Archive

 
Categories / Delphi / System
 

Determine how long Windows has been running

Title: Determine how long Windows has been running. Question: Using the windows API function GetTickCount is an easy way to determine how long the systems been up. Answer: This is the OnClick event handler for Button1. It will display a message box with text in this format: Windows started on Thursday, February 10, 2000 at 11:42:46 AM Its been up for 0 days, 3 hours, 22 minutes, 54 seconds procedure TForm1.Button1Click(Sender: TObject); var ndays: double; ticks: LongInt; btime: TDateTime; begin {The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started.} ticks := GetTickCount; {to convert this to the number of days, divide by number of milliseconds in a day, 24*60*60*1000=86400000} ndays := ticks/86400000; {to calculate the boot time we subtract the number-of-days-since-boot from the DateTime now. This works because a TDateTime is a double value which holds days and decimal days} bTime := now-ndays; {display the message} ShowMessage( FormatDateTime('"Windows started on" dddd, mmmm d, yyyy, ' + '"at" hh:nn:ss AM/PM', bTime) + #10#13 + 'Its been up for ' + IntToStr(Trunc(nDays)) + ' days,' + FormatDateTime(' h "hours," n "minutes," s "seconds"',ndays)); end;