# [Lazarus] DateDif function needed

waldo kitty wkitty42 at windstream.net
Wed Nov 13 16:22:04 CET 2013

```NOTE: subject fixed to indicate the actual topic ;)

On 11/13/2013 6:24 AM, Lex Edmonds wrote:
> This is my first post on this mailing list, so please bear with me.
> I used to do date calculations like this in the '80s when I was writing Real
> Estate software.
>
> On Tue, 12 Nov 2013 20:33:00, waldo kitty wrote:
>
>     [...]
>>    the real question is this:
>>
>>        what do we count? the starting day, the ending day, both days or neither
>>    day?
>
>
> There are two types of days-between-dates calculations: Inclusive and Exclusive.
>
> Generally, the starting day should be considered the first day of the calculation.
>
> With an Inclusive calculation, the ending day is counted.
> With an Exclusive calculation, the ending day is not counted -- it is considered
> to be the first day of the following period.

agreed and as you note later, two different types of the "base"... either "true
interval" or "calendar interval"...

> In Real Estate for example, date calculations are Inclusive.
> So a 1-week rent payment that starts on Monday 1 January 2007 ends on Sunday 7
> January 2007.
> Therefore an Inclusive DateDiff calculation between 1/01/2007 and 7/01/2007
> would return 7 days.
> Whereas an Exclusive calculation would return 6 days.

agreed...

> Similarly, when calculating a person's age.
> A baby born on 1/01/2007 would be 7 days old on 7/01/2007.

true :)

>>    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...
>
>
> This is a topic that has some tricky boundary conditions.

the boundaries are what we are trying to determine ;)

> Generally the rule is easy for days 1-27 in every month:
> The Inclusive DateDiff between day M in month 1 and Day M-1 in month 2 returns 1
> month.
> e.g. between 28/01/2007 and 27/02/2007.

yes...

> However the rules get muddy with days 28-31. i.e. the "last few" days of every
> month.

yes... in "calendar" counting, a full month is recorded when DoM1 >= EoM2

> For example, an Inclusive DateDiff calculation between 29/01/2007 and 28/02/2007
> should return 1 month.
> In my opinion, an Inclusive calculation between 30/01/2007 and 28/02/2007 should
> also return 1 month.
> And so should an Inclusive calculation between 31/01/2007 and 28/02/2007.

openOffice 4 seems to agree with you when using "calendar" counting ;)

the first 12 are leapyear and the last six are non-leapyear...

Start           End             Days    Months       Months
(interval)   (calendar)
2000-01-27      2000-02-28      32      1            1
2000-01-28      2000-02-28      31      1            1
2000-01-29      2000-02-28      30      0            1
2000-01-30      2000-02-28      29      0            1
2000-01-31      2000-02-28      28      0            1
2000-02-01      2000-02-28      27      0            0

2000-01-27      2000-02-29      33      1            1
2000-01-28      2000-02-29      32      1            1
2000-01-29      2000-02-29      31      1            1
2000-01-30      2000-02-29      30      0            1
2000-01-31      2000-02-29      29      0            1
2000-02-01      2000-02-29      28      0            0

2001-01-27      2001-02-28      32      1            1
2001-01-28      2001-02-28      31      1            1
2001-01-29      2001-02-28      30      0            1
2001-01-30      2001-02-28      29      0            1
2001-01-31      2001-02-28      28      0            1
2001-02-01      2001-02-28      27      0            0

> In fact, we used to call these "calendar months" to differentiate them from
> "other" months that might always be defined as having perhaps 30 days.
>
> So as you see, there is no real hard answer to your question. It probably depend
> on what you are trying to achieve.

i think that the existing routines are two variations on "interval" (inclusive
and exclusive) and this discussion is about getting "calendar" routines in
place... so far we seem to have something close to a working
CalendarDateDiff(D1,D2: TDateTime) :)

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

```