[Lazarus] DateDif function needed

John Landmesser JohnML at online.de
Sat Nov 9 20:47:01 CET 2013


On 08.11.2013 21:35, John Landmesser wrote:
> Hi List,
>
> i'm searching a pascal datetime function that simulates an Excel 
> function: "DateDif"
>
> Excel for example knows the function DateDif that returns the number 
> of Years, month and days between Date1 and date2.
>
> Date1 := 21.12.2012
> Date2 := 01.01.2013
>
> Result would be: 0 years, 0 moths, 11 days
>
> I tried to do it on my own, but without success.
>
> But i think there is a function out there but i don't know its name :-((
>
> Thanks for your tipps
>
> John
>
> -- 
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


Found a solution  in Jedi: /jvcl/run/JvJCLUtils.pas

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;

**************************************

Seems to calculate ok.
Now i'll try to understand what it is doing :-))


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20131109/6eb7794a/attachment-0003.html>


More information about the Lazarus mailing list