[Lazarus] DateDif function needed

waldo kitty wkitty42 at windstream.net
Sat Nov 9 17:30:24 CET 2013


On 11/9/2013 8:32 AM, leledumbo wrote:
> Indeed that was wrong, I shouldn't exploit the internal details. Below is the
> correct one (still no need for that complicated code of yours):
>
> uses
>    SysUtils,DateUtils;
> var
>    Date1,Date2: TDate;
>    Y,M,D: Word;
> begin
>    Date1 := EncodeDate(2012,12,21);
>    Date2 := EncodeDate(2013,01,01);
>    Y := YearsBetween(Date1,Date2);
>    M := MonthsBetween(Date1,Date2);
>    D := DaysBetween(Date1,Date2);
>    WriteLn(Y,'-',M,'-',D);
> end.

disregarding the above writeln being incorrect for the values represented, even 
this has problems... each value is the total value between the dates... 
YearsBetween and MonthsBetween are short by 1...


2013/01/01
2013/01/01
0 yrs, 0 mos, 0 wks, 0 dys

above is correct (of course)...

2013/01/01
2014/01/01
0 yrs, 11 mos, 52 wks, 365 dys

above should be 1 yrs, 12 mos... weeks and days are correct...

2013/01/01
2015/01/01
1 yrs, 23 mos, 104 wks, 730 dys

above should be 2 yrs, 24 mos... weeks and days are correct...


program DateDiff;

uses
   SysUtils,DateUtils;

var
   Date1,Date2: TDate;
   Yr,Mo,Wk,Dy: Integer;

begin
   Date1 := EncodeDate(2013,01,01);
   Date2 := EncodeDate(2015,01,01);
   writeln(FormatDateTime('YYYY/MM/DD HH:NN:SS.ZZZ',Date1));
   writeln(FormatDateTime('YYYY/MM/DD HH:NN:SS.ZZZ',Date2));
   Yr := YearsBetween(Date1,Date2);
   Mo := MonthsBetween(Date1,Date2);
   Wk := WeeksBetween(Date1,Date2);
   Dy := DaysBetween(Date1,Date2);
   writeln(Yr,' yrs, ',Mo,' mos, ',Wk,' wks, ',Dy,' dys');
end.


-- 
NOTE: No off-list assistance is given without prior approval.
       Please keep mailing list traffic on the list unless
       private contact is specifically requested and granted.




More information about the Lazarus mailing list