Search code examples
delphidatetimetime

convert HH:MM:SS to seconds or minutes with delphi


Can someone advise me on the best way to convert a duration formated HH:MM:SS to seconds or minutes only?

ie. 00:01:49 = 109secs

I´m using this code but not work

var
i: real;
j: integer;
begin
i := frac(real(StrToTime('00:00:01')));
j := trunc(frac(real(StrToTime('01:00:00'))) / i );
memo2.lines.add(inttostr(j));

when I try the code with 00:10:00 return 599

thanks


Solution

  • A very flexibel tool for handling durations is TTimeSpan found in unit System.TimeSpan. You can get the result in different units (second, minutes, hours, ...) and format that to your needs.

    var
      timeSpan: TTimeSpan;
    begin
      timeSpan := TTimeSpan.Parse('00:01:49');
      memo2.lines.add(Format('%1.1f min', [timeSpan.TotalMinutes]));
    end;