This directly relates to this original thread from 2014 (
http://forum.lazarus.freepascal.org/index.php?topic=25000.0), but, I did get an answer to that original question, thus, a new thread.
I want to display the time that has elapsed between start date and time and end date and time as "X days, X hours, H mins, and x seconds". It works fine for an elapsed time up to 24 hours, but after that it seems to reset. So if something takes three days and finishes at 08:30 in the morning, the elapsed time is rendered as "8 hrs, 30 min, 20 sec" instead of "3 days, 8 hrs, 30 min, 20 sec"
var
StartTime, EndTime, TimeDifference : TDateTime;
strTimeDifference : string;
begin
// Compute the end time and how long it took.
Startime := Now;
lblStartTime.Caption := FormatDateTime('DD/MM/YY HH:MM:SS', StartTime);
// some time later...
EndTime := Now;
lblEndTime.Caption := FormatDateTime('DD/MM/YY HH:MM:SS', EndTime);
TimeDifference := EndTime - StartTime;
strTimeDifference := FormatDateTime('h" hrs, "n" min, "s" sec"', TimeDifference);
lblTimeTaken.Caption := strTimeDifference;
end;
So to achieve "3 days, 8 hrs, 30 min, 20 sec", which I accept the original idea does not extend to, I tried an adjustment :
strTimeDifference := FormatDateTime('d" days, "h" hrs, "n" min, "s" sec"', TimeDifference);
but now, if something takes 10 seconds, I get the following output:
"30 days, 0 hrs, 0 min, 10 sec"
so I have no idea where the 30 days is coming from. I'm obviously not getting TDateTime properly but I assumed that if EndTime was 48 hours after StartTime, then strTimeDifference would would equal 48 hours. So when formatted with FormateDateTime, I assumed would become "2" (2 x 24 hours).
Where am I going wrong?