I am creating this function:
function LiteralTimePassed(FromDate: TDateTime; ToDate: TDateTime = 0): string;
const
Coma = ', ';
var
Dt, Mt: Integer; { se dos dias contarem mais que 30/31 então aumenta o mês }
P: TDateTime;
HC: Boolean; { indica se já há um token antes do novo token, para colocar vírgula }
Token: string; { a parte do timestamp verificada no loop }
Literal: string;
Y, M, D, H, N, S: Integer; { ano, mês, dia, hora, minuto(n), segundo }
Ys, Ms, Ds, Hs, Ns, Ss: Boolean; { valida se valores maiores que 1 para adicionar 's' }
begin
{ retorna quanto tempo se passou desde a data 1 até a data 2 em forma literal }
if ToDate = 0 then ToDate := Now;
HC := False;
Literal := '';
P := ToDate-FromDate ;
Dt := (DaysInMonth(FromDate)-DayOf(FromDate))+(DaysInMonth(ToDate)-DayOf(ToDate));
Mt := Dt div DaysInMonth(ToDate);
Ys := VarAssign(Y, YearsBetween(ToDate, FromDate)) > 1;
Ms := VarAssign(M, (MonthsBetween(ToDate, FromDate)-(Y*MonthsPerYear))-Mt) > 1;
Ds := VarAssign(D, Dt mod DaysInMonth(ToDate)) > 1;
// I did not make the hour, minute and second, yet...
end;
To get a response like:
There has been "2 years, 4 months, 1 day, 7 hours, 30 minutes and 22 seconds" between those dates
My doubt is if my logic is correct in the case of counting days passed and if I will have to make the same calculations with the time part.
But if you know any free coded that already does that, it will save me much time!
Thanks.
First deal with years and months, after that TTimeSpan:
I'd use the following logic:
Regarding the day and time part you can ease your life with TTimeSpan found in System.TimeSpan:
This will not solve the year, month part, though.