On 12.11.2013 13:05, Bart wrote:

I proposed a "solution" in this thread.
It'll give you (at least that was the intention) the years, months,
and days between two (Gregorian) dates.

Bart

--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Hi Bart,

i found this in old RxLib ( now Yedi ).

Perhaps it inspires you.

procedure DateDiff(Date1, Date2: TDateTime; var Days, Months, Years: word);
var
  DtSwap: TDateTime;
  Day1, Day2, Month1, Month2, Year1, Year2: word;
begin
  if Date1 > Date2 then
  begin
    DtSwap := Date1;
    Date1 := Date2;
    Date2 := DtSwap;
  end;
  DecodeDate(Date1, Year1, Month1, Day1);
  DecodeDate(Date2, Year2, Month2, Day2);
  Years := Year2 - Year1;
  Months := 0;
  Days := 0;
  if Month2 < Month1 then
  begin
    Inc(Months, 12);
    Dec(Years);
  end;
  Inc(Months, Month2 - Month1);
  if Day2 < Day1 then
  begin
    // von mir auskommentiert Inc(Days, DaysPerMonth(Year1, Month1));
    Inc(Days, DaysInAMonth(Year1, Month1));
    if Months = 0 then
    begin
      Dec(Years);
      Months := 11;
    end
    else
      Dec(Months);
  end;
  Inc(Days, Day2 - Day1);
end;


My Tests showed: this procedure works well !

The next function seems also much better than that in DateUtil:

function MonthsBetween(Date1, Date2: TDateTime): Double;
var
  D, M, Y: Word;
begin
  DateDiff(Date1, Date2, D, M, Y);
  Result := 12 * Y + M;
  if (D > 1) and (D < 7) then Result := Result + 0.25
  else if (D >= 7) and (D < 15) then Result := Result + 0.5
  else if (D >= 15) and (D < 21) then Result := Result + 0.75
  else if (D >= 21) then Result := Result + 1;
end;

Problem: it returns Double!!

I'll use these two and i think everything is ok?!!

happy hacking ...

John

--
_______________________________________________
Lazarus mailing list
[email protected]
http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply via email to