Author: Tomas Rutkauskas
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:
1 procedure TMyForm.Button1Click(Sender: TObject);
2 var
3 StartT, EndT, DeltaT: TDateTime;
4 Days, Hour, Min, Sec, MSec: Word;
5 begin
6 StartT := StrToDateTime('27.02.2000 13:45');
7 EndT := StrToDateTime('02.03.2000 17:30');
8 DeltaT := EndT - StartT;
9 Days := trunc(DeltaT);
10 DecodeTime(DeltaT, Hour, Min, Sec, MSec);
11 ShowMessage(' Time elapsed: ' + IntToStr(Days) + ' days, ' + IntToStr(Hour) +
12 ' hours, ' + IntToStr(Min) + ' minutes ');
13 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.
|