# [Lazarus] DateDif function needed

waldo kitty wkitty42 at windstream.net
Sat Nov 9 18:42:07 CET 2013

```digging deeper to work directly with YearsBetween and MonthsBetween shows that
they seem to be missing the usual "+0.5" generally used to ensure that rounding
is properly handled...

the following two pairs of routines work to give the desired output...

*this pair uses Trunc((X)+.05) instead of the original Trunc(X)*

Function YearsBetween(const ANow, AThen: TDateTime): Integer;
begin

Result:=Trunc(((Abs(MyDateTimeDiff(ANow,AThen))+TDateTimeEpsilon)/ApproxDaysPerYear)+0.5);
end;

Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
begin

Result:=Trunc(((Abs(MyDateTimeDiff(ANow,AThen))+TDateTimeEpsilon)/ApproxDaysPerMonth)+0.5);
end;

note the additional '(' after 'Trunc' and the closing '+0.5)' which handles the
rounding as compared to the original routines in DateUtil.inc...

*OR this pair using Round() instead of the original Trunc(X) or the above
Trunc((X)+0.5)*

Function YearsBetween(const ANow, AThen: TDateTime): Integer;
begin

Result:=Round((Abs(MyDateTimeDiff(ANow,AThen))+TDateTimeEpsilon)/ApproxDaysPerYear);
end;

Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
begin

Result:=Round((Abs(MyDateTimeDiff(ANow,AThen))+TDateTimeEpsilon)/ApproxDaysPerMonth);
end;

i don't know which would be better to use in this case but one should be applied
to the existing routines in DateUtil.inc to fix the error in them ;)

--
NOTE: No off-list assistance is given without prior approval.
Please keep mailing list traffic on the list unless
private contact is specifically requested and granted.

```