Link to home
Start Free TrialLog in
Avatar of shaneholmes
shaneholmes

asked on

Difference between two TDatetimes - formatted as string

I need a fuction which takes a StartTime (TDateTIme) and EndTime (TDateTIme) and returns the difference formatted as such 'Hours:Mins:Secs'

function GetDiffStr(End, Start: TDateTime): String
begin
 result:=
end;
ASKER CERTIFIED SOLUTION
Avatar of Trekker72
Trekker72

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kabix
kabix

Check out: http://www.bsdg.org/swag/DELPHI/0258.PAS.html

function GetDiffStr(End, Start: TDate): String
begin
 DateSeparator:= '-';
 ShortDateFormat:= 'm/d/yyyy';
 aDay:=StrToDate('9-23-1976');
 bDay:=strToDate('10-23-1976');
 result:= intToStr(daysBetween(aDay,bDay)));
end;

Also you could check: http://beta.experts-exchange.com/questions/20672746/Days-between-two-dates.html

If it helped please dont forget to accept the answer.
A TDateTime datatype is nothing but a wrapper around a double datatype.
this means that taking the difference between two TDateTime variables is just substract the one from the other

Diff := Date1 - Date2;

remember to have the 'DateUtils' in your uses clause

function GetDiffStr(End, Start: TDateTime): String
var
Diff: TDateTime;
begin
Diff := Start - End;
result:= TimeToStr(Diff);
end;


TimeToStr converts a TDateTime variable to a string, but ONLY the time part. If you want the exact difference with days too, use the DateTimeToStr(Diff) instead!

hope this helps

Helgesen