[Lazarus] DateDif function needed

John Landmesser JohnML at online.de
Tue Nov 12 21:40:28 CET 2013


On 12.11.2013 21:01, waldo kitty wrote:
> type
>   Date_Diff = record
>                 Years,
>                 Months,
>                 Days: Word;
>               end;
>
> function CalendarDateDiff(Date1,Date2: TDateTime): Date_Diff;
> var
>   theDiffRec: Date_Diff;
>   Cmp: Integer;
>   loDate,hiDate: TDateTime;
>   loYear,hiYear,loMonth,hiMonth,loDay,hiDay: Word;
> begin
>   FillChar(theDiffRec,SizeOf(theDiffRec),0);  // init results to zero
>   Cmp:=CompareDateTime(Date1,Date2);          // compare dates
>   If Cmp<0 then
>   begin
>     loDate:= Date1;                           // and set loDate to oldest
>     hiDate:= Date2;
>   end
>   else if Cmp>0 then
>   begin
>     loDate:= Date2;                           // and set loDate to oldest
>     hiDate:= Date1;
>   end;
>   DecodeDate(loDate,loYear,loMonth,loDay);
>   DecodeDate(hiDate,hiYear,hiMonth,hiDay);
>   theDiffRec.Years:= hiYear - loYear;
>   if (loMonth > hiMonth) or ((loMonth = hiMonth) and (loDay > hiDay)) 
> then
>   begin
>     theDiffRec.Years:= theDiffRec.Years - 1;
>   end;
>   if loMonth > hiMonth then
>   begin
>     hiMonth:= hiMonth + 12;
>   end;
>   theDiffRec.Months:= hiMonth - loMonth;
>   if hiDay >= loDay then
>   begin
>     theDiffRec.Days:= hiDay - loDay
>   end
>   else
>   begin
>     if theDiffRec.Months = 0 then
>     begin
>       theDiffRec.Months:= 11;
>     end
>     else
>     begin
>       theDiffRec.Months:= theDiffRec.Months - 1;
>     end;
>     theDiffRec.Days:= DaysInAMonth(hiYear,loMonth) - loDay + hiDay;
>   end;
>   Result:= theDiffRec;
> end; 

Which is correct?

Date1 := 29.2.2000
Date2 := 28.02.2001

Your function:
0 Y, 11 M, 27 D

Rxlib ( Jedi ) DateDiff:
0 Y, 11 M, 28 D

Libre Office Calc:
0 Y, 11 M, 30 D

The table - function of Libre Office Calc is called in german DATUMDIF()

Get a calendar and count??








More information about the Lazarus mailing list