/ Published in: Delphi
SecondsToTimeString(Seconds: Int64): string;
Convert a seconds integer to time string like: '4m 35s' or '2 month 21 day'
Convert a seconds integer to time string like: '4m 35s' or '2 month 21 day'
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function SecondsToTimeString(Seconds: Int64): string; const itemNames: TTimeItemNames = ('year', 'month', 'day', 'h', 'm', 's'); begin result := SecondsToTimeString(Seconds, itemNames); end; function SecondsToTimeString(Seconds: Int64; const itemNames: TTimeItemNames): string; const divisors: array [0..5] of Int64 = (SecsPerDay * 365, SecsPerDay * 31, SecsPerDay, SecsPerHour, SecsPerMin, 1); var resCount: integer; I: Integer; C, V: Int64; begin result := ''; resCount := 0; C := Seconds; for I := Low(divisors) to High(divisors) do begin V := C div divisors[I]; if V > 0 then begin if resCount > 0 then result := result + ' '; result := result + IntToStr(V) + itemNames[I]; Inc(resCount); if resCount > 1 then break; C := C mod divisors[I]; end; end; end;