Title: How to Convert an amount of Milliseconds to a TDateTime Value
When doing lengthy operations in your Delphi applications, you might want to display the time passed to the user.
Here's an example:
var
startTick, endTick : Int64;
begin
startTick := GetTickCount;
SomeLengthyFunction() ;
endTick := GetTickCount;
ShowMessage(MSecToTime(endTick - startTick)) ;
The Windows API function GetTickCount retrieves the number of milliseconds that have elapsed since Windows was started.
To get the number of milliseconds that passed while the SomeLengthyFunction was executing you need to subtract endTick value from the startTick value.
Here's a function that takes an amount of milliseconds, converts those milliseconds to days, hours, minutes and seconds and returns the string representation:
function MSecToTime(mSec: Int64): string;
var
dt : TDateTime;
begin
dt := mSec / MSecsPerSec / SecsPerDay;
Result := Format('%d days, %s', ) ;
end;
Note: The MSecsPerSec and SecsPerDay constants are defined in the SysUtils unit.