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(ANow, AThen: TDateTime): Integer;
Var Yn, Yt, Mn, Mt, Dn, Dt: Word;
Begin
CheckedSwap(ANow, AThen);
DecodeDate(ANow, Yn, Mn, Dn);
DecodeDate(AThen, Yt, Mt, Dt);
Result := Yt - Yn;
If (Mt < Mn)
or ((Mt = Mn)
and ((Dt < Dn)))
Then Dec(Result);
End;
Function MonthsBetween(ANow, AThen: TDateTime): Integer;
Var Yn, Yt, Mn, Mt, Dn, Dt: Word;
Begin
CheckedSwap(ANow, AThen);
DecodeDate(ANow, Yn, Mn, Dn);
DecodeDate(AThen, Yt, Mt, Dt);
Result := (Yt - Yn) * 12 + (Mt - Mn);
If Dt < Dn
Then Dec(Result);
End;