How to get the difference between two dates in days and hours?

Hello Guys

I have two variable in this format:  dd/mm/yyyy hh:mm:ss

StartDate = '01/11/2017 13:00:00'  
EndDate  = '02/11/2017  13:30:00'

How to get the difference between these two dates in days and hours

ddd  hh:mm:ss
001   01:30:00

Thanks
Alex
LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
Alex

I'm sure there are better ways, bit rusty with Delphi/Pascal, but this worked for me.
uses
  System.SysUtils, DateUtils, Math;


var StartDate, EndDate, ElapsedTime :TDateTime;
    Dy, Hr,Mn,Sc, Msc:Word;
begin

  StartDate:=encodedatetime(2017,11,1,13,00,00,00);
  EndDate:=encodedatetime(2017,11,2,13,30,00,00);

  Dy:=daysbetween(StartDate, EndDate);

  ElapsedTime:= EndDate-StartDate;

  DecodeTime(ElapsedTime,Hr,Mn,Sc,Msc);

  writeln(Output, inttostr(Dy)+' days '+inttostr(Hr)+' hours ' + inttostr(Mn) +' mins');

  readln;

end.

Open in new window

Sinisa VukSoftware architectCommented:
Which Delphi version you use?
In older (<XE) I would go with:
function GetMyDate(sDate: String): TDateTime;
var
  tmpDTFormat,tmpTMFormat: String;
  tmpDTSeparator: Char;
begin
  tmpDTFormat:=ShortDateFormat;
  tmpTMFormat:=ShortTimeFormat;
  tmpDTSeparator:=DateSeparator;
  try
    ShortDateFormat:='dd/mm/yyyy';
    ShortTimeFormat:='hh:nn:ss';
    DateSeparator:='/';
    Result:=StrToDateTime(sDate);
 finally
    ShortDateFormat:=tmpDTFormat;
    ShortTimeFormat:=tmpTMFormat;
    DateSeparator:=tmpDTSeparator;
  end;
end;

...
dtStart:=GetMyDate('01/11/2017 13:00:00');
dtEnd:=GetMyDate('02/11/2017 13:30:00');

ddd := DaysBetween(dtEnd, dtStart);
dtEnd := IncDay(dtEnd, -ddd);

hh := HoursBetween(dtEnd, dtStart);
dtEnd := IncHour(dtEnd, -hh);

nn := MinutesBetween(dtEnd, dtStart);
dtEnd := IncMinute(dtEnd, -nn);

ss := SecondsBetween(dtEnd, dtStart);
...

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert GOracle dbaCommented:
a date is stored as a float in Delphi
(not the text representation)
the fractional part is the time, the whole part is the day

with a little math:
dt := encodedatetime(2017,11,2,13,30,00,00) - encodedatetime(2017,11,1,13,00,00,00);

days := trunc(dt);
hours := trunc((dt - trunc(dt)) * 24);
minutes := trunc((dt - trunc(dt) - hours/24) * 60);
seconds := trunc((dt-trunc(dt) - hours/24 - minutes/24/60) * 60;

Open in new window

hidrauAuthor Commented:
thanks a lot for the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.