MonthsBetween returns the approximate number of months between two specified TDateTime values.

Because months are not all the same length, MonthsBetween returns an approximation based on an assumption of 30.4375 days per month (365.25/12).

Fractional months are not counted, so the function will return a whole integer value only.

For example, MonthsBetween reports the difference between 1st February and 1st March as one (1), but the difference between 1st March and 31st March is zero(0).

The ordering of the two dates doesn't matter - the number of months will not be negative.

Declaration: Function MonthsBetween(const ANow: TDateTime; const AThen: TDateTime): Integer

Examples follow.

procedure OnMapEvent(var Value:Variant);
var
vYear, vDate : TDateTime;
vStrYear, vStrDate : string;
vBetwix : integer;
begin
//Result is 3 whole months
vBetwix := MonthsBetween(Date, IncDay(Date, 101));
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 27 whole months
vBetwix := MonthsBetween(Date, IncMonth(Date, 27));
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 6 whole months
vStrDate := '01-10-2023';
vStrYear := '01-04-2024';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := MonthsBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 11 whole months
vStrDate := '01-01-2023';
vStrYear := '31-12-2023';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := MonthsBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 12 whole months
vStrDate := '01-01-2024';
vStrYear := '01-01-2025';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := MonthsBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 12 whole months
vStrDate := '01-01-2023';
vStrYear := '25-01-2024';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := MonthsBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 50 whole months
vStrDate := '01-01-2023';
vStrYear := '25-03-2027';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := MonthsBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
//Result is 0 whole months
vStrDate := '01-01-2023';
vStrYear := '25-01-2023';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYear := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := MonthsBetween(vDate, vYear);
LogInfo(IntToStr(vBetwix));
LogInfo('');
 
end;