[Lazarus] DateDif function needed

waldo kitty wkitty42 at windstream.net
Tue Nov 12 21:03:29 CET 2013



*FWIW*

On 11/11/2013 5:11 PM, Bart wrote:

> type
>    TDaysPerMonth = Array[1..12] of Word;
>
>
> function DaysPerMonth(AMonth: Word; IsLeapYear: Boolean): Word;
> const
>    DaysPerMonthNormal: TDaysPerMonth = (31,28,31,30,31,30,31,31,30,31,30,31);
>    DaysPerMonthLeap:   TDaysPerMonth = (31,29,31,30,31,30,31,31,30,31,30,31);
> begin
>    if IsLeapYear then
>      Result := DaysPerMonthLeap[AMonth]
>    else
>     Result := DaysPerMonthNormal[AMonth];
> end;

you do not need any of the above if you change

>      Days := (DaysPerMonth(M1, IsLeapYear(Y2)) - D1) + D2 ;

to

       Days := (DaysInAMonth(Y2,M1) - D1) + D2;

this because DaysInAMonth returns the correct number of days and takes leapyear 
into account automatically... so there's no reason to duplicate existing code 
with the above type and function ;)

outside of this, what you came up with is almost exactly what i worked my way 
down to as i was working on my own solution... it took me a while and then when 
i saw your post, i was astonished at how the mind works at times :LOL:

-- 
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