2009. október 13., kedd
How to calculate the elapsed time between 2 DateTime fields
Problem/Question/Abstract:
Using Delphi 5, I need to calculate the elapsed time between 2 DateTime fields with the result returned in Days, Hours and Minutes format.
Answer:
Subtract the values AsDateTime and then use DecodeDate and DecodeTime on the result:
procedure TMyForm.Button1Click(Sender: TObject);
var
StartT, EndT, DeltaT: TDateTime;
Days, Hour, Min, Sec, MSec: Word;
begin
StartT := StrToDateTime('27.02.2000 13:45');
EndT := StrToDateTime('02.03.2000 17:30');
DeltaT := EndT - StartT;
Days := trunc(DeltaT);
DecodeTime(DeltaT, Hour, Min, Sec, MSec);
ShowMessage(' Time elapsed: ' + IntToStr(Days) + ' days, ' + IntToStr(Hour) +
' hours, ' + IntToStr(Min) + ' minutes ');
end;
When trying to actually run the above sample, you have to change the string constants used to initialize the datetime vars according to your settings in ShortDateFormat and LongTimeFormat.
Nincsenek megjegyzések:
Megjegyzés küldése