The YearSpan function returns the approximate number of years between two specified TDateTime values, including any fraction of a year.

Because years are not all the same length due to leap years, YearSpan returns an approximation based on an assumption of 365.25 days per year.

But unlike the similar function YearsBetween, the YearSpan function reports incomplete years as a fraction of the 365.25-day year.

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

Declaration: function YearSpan(const ANow, AThen: TDateTime): Double;

The following example shows several scenarios.

procedure OnMapEvent(var Value:Variant);
var
vYearT, vDate : TDateTime;
vStrYear, vStrDate : string;
vBetwix : double;
begin
//Result is 0.0848733744010951 of a year - run-date to run-date plus 1 month
//Where run-date is 03-12-2023 plus 31 days (31/365.25 = 0.0848733744010951)
vBetwix := YearSpan(Date, IncMonth(Date, 1));
LogInfo(FloatToStr(vBetwix));
 
//Result is 0.501026694045175 of a year - 6 months only or 183 days
vStrDate := '01-10-2023';
vStrYear := '01-04-2024';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYearT := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := YearSpan(vDate, vYearT);
LogInfo(FloatToStr(vBetwix));
 
//Result is 0.996577686516085 of a year - 2023 is NOT a leap year = 364 days
vStrDate := '01-01-2023';
vStrYear := '31-12-2023';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYearT := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := YearSpan(vDate, vYearT);
LogInfo(FloatToStr(vBetwix));
 
//Result is 0.999315537303217 of a year - 2024 is a leap year = 365 days
vStrDate := '01-01-2024';
vStrYear := '31-12-2024';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYearT := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := YearSpan(vDate, vYearT);
LogInfo(FloatToStr(vBetwix));
 
//Result is 1.00205338809035 of a year - whole leap year - 366 days
vStrDate := '01-01-2024';
vStrYear := '01-01-2025';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYearT := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := YearSpan(vDate, vYearT);
LogInfo(FloatToStr(vBetwix));
 
//Result is 1.00205338809035 of a year - whole year +1 - 366 days
vStrDate := '01-01-2023';
vStrYear := '02-01-2024';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYearT := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := YearSpan(vDate, vYearT);
LogInfo(FloatToStr(vBetwix));
 
//Result is 4 whole years - 1461 days
vStrDate := '01-01-2023';
vStrYear := '01-01-2027';
vDate := StringToDateTime('dd/mm/yyyy',vStrDate);
vYearT := StringToDateTime('dd/mm/yyyy',vStrYear);
vBetwix := YearSpan(vDate, vYearT);
LogInfo(FloatToStr(vBetwix));
LogInfo('');
 
end;