Procedure CheckedSwap(
Var ANow, AThen: TDateTime);
Inline;
Var Temp: TDateTime;
Begin
If ANow <= AThen
Then Exit;
Temp := ANow;
ANow := AThen;
AThen := Temp;
End;
Function YearsBetween(
const ANow, AThen: TDateTime): Integer;
Var Yn, Yt, Mn, Mt, Dn, Dt: Word;
d1, d2 : TDateTime;
Begin
d1 := ANow; d2 := AThen;
CheckedSwap(d1, d2);
DecodeDate(d1, Yn, Mn, Dn);
DecodeDate(d2, Yt, Mt, Dt);
Result := Yt - Yn;
If (Mt < Mn)
or ((Mt = Mn)
and ((Dt+Frac(d2) < Dn+Frac(d1))))
Then Dec(Result);
End;
Function MonthsBetween(
const ANow, AThen: TDateTime): Integer;
Var Yn, Yt, Mn, Mt, Dn, Dt: Word;
d1, d2 : TDateTime;
Begin
d1 := ANow; d2 := AThen;
CheckedSwap(d1, d2);
DecodeDate(d1, Yn, Mn, Dn);
DecodeDate(d2, Yt, Mt, Dt);
Result := (Yt - Yn) * 12 + (Mt - Mn);
If Dt+Frac(d2) < Dn+Frac(d1)
Then Dec(Result);
End;