[Lazarus] DateDif function needed

Michael Van Canneyt michael at freepascal.org
Sat Nov 16 18:40:23 CET 2013



On Sat, 16 Nov 2013, Reinier Olislagers wrote:

> On 16/11/2013 18:21, Michael Van Canneyt wrote:
>> On Fri, 15 Nov 2013, Bart wrote:
>>> The fun part for me is the fact that a seemingly simple question,
>>> where at first glance you would think "I'll just implement that", can
>>> lead to so many "problems".
>>
>> I added a modified version of your implementation to the DateUtils unit as:
>>
>> Procedure PeriodBetween(Const ANow, AThen: TDateTime; Out Years, months,
>> days : Word);
>>
>> I chose this name because it is more in line with the existing
>> XXXBetween functions.
>>
>> I did change the algorithm so it does not do the correction for february
>> of the leap year.
>> It returns now the same results as the Jedi function. (which I didn't
>> take for license reasons)
>>
>> Thanks for providing an implementation.
>
>
> A suggestion if I may: given the wide variation of possibilities of
> interpretation, some comments in the code about how the calculation is
> supposed to work would be very much appreciated, I'm sure.
> (Of course, that applies even more to added documentation).
>
> Just wanted to avoid another huge thread discussing PeriodBetween in
> future... as well as confusion on the part of possible users in general.

When documenting, all will be explained.

I think it's fairly simple, really.

Start with D1. Call this D.
Keep adding 1 to year of D till your jumps over D2. 
If you jumped over, substract 1 from year.
Keep adding 1 to month of D till D jumps over D2. 
If you jumped over, substract 1 from month.
Keep adding 1 to day of D till D reaches D2.

The number of years, months, days you thus added is the period. 
I may fiddle a bit with the definition of "jumps over" and "reaches", but essentially that is it.

Michael.




More information about the Lazarus mailing list