13

I am trying to get the difference between two datetimes and display it in string as hh:mm

q.parambyname('vstart').asdatetime:=  vstart;
q.parambyname('vend').asdatetime:= vend;
d:= vend-vstart;
mins:= d * 1440;
q.ParamByName('mins').asBCD:= mins;

currently the database stores it in minutes

example (0.39)

I would like to then take it from database and display it in the string format hh:mm

Blow ThemUp
  • 895
  • 6
  • 18
  • 29
  • 6
    Do you really need `mins` field in your Database? `vstart` and `vend` fields already contain all information you need to return difference between them as query result (in format you need). – teran Oct 23 '12 at 13:46

2 Answers2

19

In DateUtils there is a function MinutesBetween which can be used as such:

m := MinutesBetween(vend,vstart);
yourHMStr := Format('%2.2d:%2.2d',[m div 60,m mod 60]);
LU RD
  • 34,438
  • 5
  • 88
  • 296
  • This answer gives hh:mm but What about seconds as it mention in the Title? – Dreamer64 Jul 12 '18 at 22:27
  • 1
    @Dreamer64, use [System.DateUtils.SecondsBetween](http://docwiki.embarcadero.com/Libraries/en/System.DateUtils.SecondsBetween) . `s := SecondsBetween(vend,vstart); HMSstr := Format('%2.2d:%2.2d:%2.2d',[s div 3600,(s div 60) mod 60,s mod 60]);`, where s is an `Int64` variable. – LU RD Jul 13 '18 at 05:54
5

I can propose this simple code using DateUtils:

DiffTimeStr:= FormatDateTime('hh:nn:ss', TimeEnd - TimeStart);
Dreamer64
  • 923
  • 2
  • 13
  • 30