# [Lazarus] DateDif function needed

waldo kitty wkitty42 at windstream.net
Wed Nov 13 14:02:01 CET 2013

```On 11/13/2013 5:19 AM, John Landmesser wrote:
> On 13.11.2013 02:33, waldo kitty wrote:
>>
>> actually, i have in some cases... let's ask this question and see what your
>> charts and routines return as the result... assuming your format is DD.MM.YYYY...
>>
>> Date1 := 01.01.2000
>> Date2 := 01.01.2000
>>
>> should be 0 (zero), right?
>>
>> then
>>
>> Date1 := 01.01.2000
>> Date2 := 02.01.2000
>>
>> should be 1 (one), right?
>>
>> in this case, what i (and bart, too) wrote, we're not counting the starting
>> day but we are counting the ending day... this rule stays in effect throughout
>> the entire process... my head still aches from rummaging about doing this...
>> it was good exercise and one i remember being given on a test many years back
>> to determine one's coding level... at that time, the code was being done in
>> dBase II/III/IV ;)
>>
>> i will have to dig deeper and see what is crossing things up for count from
>> 29.02.2000 to 28.02.2001... i see bart has posted a fix which i have not yet
>> had a chance to compare with my code or run thru the tests i included in my
>> posting...
>>
>> the real question is this:
>>
>>   what do we count? the starting day, the ending day, both days or neither day?
>>
>> then we have another question:
>>
>>   is dayM in monthY a one month difference to dayM in monthX and
>>   monthZ? monthX, monthY, monthZ are any three consecutive months
>>   in linear order within one year or crossing two years
>>
>> once the rules are chosen, then we have something to work with...
>>
>
> I'm just a hobbyist, never went to university to study computer science.

i never went to school for any of my computer skills, either... i'm self taught
in both hardware and software ;)

> We don't need to invent the wheel again, because others have solutions for that!

then why does this thread exist? O:)

> Perhaps its solved in " The Art of/Computer Programming/" from Donald E. Knuth?
>
> I don't have such books and i don't know if i'd have the chance to understand
> such a book?!!

i don't know as i do not have that book... but since you have the three routines
available, can you please post what they return for the two cases above??

[...]
> Bart delivered code that calculates 29.02.2000 to 28.02.2001 = 1 Year.
> And that looks reasonable to me!

but it is not, not really... 29.02 only comes around once every four years...
29.02.leapyear to 28.02.leapyear+1 is one day short of a year... if you were
born on 29.02.1960, have you celebrated your birthday 13 or 53 times? :)

> Bart wrote:
>> I would actually say that in this particular case the diff is 1 Year...
>> (11 M + (28 days in feb in a non-leapyear = 1M) = 12M = 1 Y.
> Soon we'll have working DateDiff function and my question will be answered in
> the end!
> Thanks guys!!

i can, however, agree with the reasoning bart has given above... do you agree
with it? is that what you want to see?

which of the following is what you want to see??

2000-02-29 to 2001-02-28 is    0 yrs   11 mos   27 days  (original)

2000-02-29 to 2001-02-28 is    0 yrs   11 mos   28 days  (bart's fix)

2000-02-29 to 2001-02-28 is    1 yrs    0 mos    0 days  (actually desired?)

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

```