The problem is not with the VCL at all.  The following code:

var CDate: TDateTime;

CDate := Date - DOB;

is incorrect.  Subtracting one date from another does not yield a date, but
rather an interval - a period of time, best expressed as a number.  So the
code above is really doing something like:

var CInterval: double;
    CDate: TDateTime;

CInterval := Date - DOB; // Interval in number of days, including fraction
CDate := TDateTime(CInterval); // This is wrong - you can't cast an interval
of time to a date sensibly

only a little more concisely.  Subtracting 1900 will work for some values,
but since Delphi will (correctly) decode the year as the year given
CInterval days since 30/12/1899, that won't necessarily correlate to the
number of years from your birth date to the current date, due to leap year
differences between the periods "30/12/1899 to 30/12/1899 + CInterval days"
and "DOB to CurrentDate".  Thus you'll find you're a day out here and there.

For a whole numbers of identical 365.25 day "years", use
Trunc(CInterval/365.25).  For age as we usually refer to the term, Greg
Nixon's GetAge function is correct.

Cheers,
Carl

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Friday, 17 December 1999 20:19
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Date/Time calculations
> 
> 
> Patrick, yes,
> 
> Unfortunately, the real solution is to recompile the vcl with the 
> fix.  Because its a number of controls that are affected (not just 
> the age calculation).... such as display/modification of dob in a 
> grid etc.
> 
> What I will be doing is to replace a whole chunk of vcl code by the 
> process of intervention.  I have deferred it for the holidays.
> 
> On 16 Dec 99 at 17:21, Patrick Dunford wrote:
> 
> > Has anyone else experienced problems with date/time 
> calculations due to
> > Delphi offsetting the date by 30/12/1899
> > 
> > The situation is that I wish to calculate a person's 
> current age which
> > appeared to be simple: subtract their date of birth from 
> the current date
> > then decode into actual years.
> > 
> > But Delphi insists on adding this offset to the date so I 
> end up with a
> > person having an age of 1901 years.
> > (Delphi 3)
> > 
> > Code below:
> > 
> > function CurrentAge(DOB:TDateTime):integer; //Return years only
> > var y,m,d:word;
> >     CDate:TDateTime;
> > begin
> >      CDate:=Date-DOB;
> >      DecodeDate(CDate,y,m,d);
> >      Result:=y;
> > end;
> > 
> > The date subtraction is fine and in one example produces a 
> result of 374
> > days, just over 1 year, Delphi then makes this into year 
> 1901 but by the
> > calculation the actual number stored in the TDateTime is 
> 1.0 something,
> > which it adds 30/12/1899 to
> > 
> > The best I have come up with is to subtract 1900 from the 
> year but this will
> > be 1 day out.
> > 
> > 
> > 
> --------------------------------------------------------------
> -------------
> >     New Zealand Delphi Users group - Delphi List - 
> [EMAIL PROTECTED]
> >                   Website: http://www.delphi.org.nz
> > 
> Rohit
> 
> ======================================================================
> CFL - Computer Fanatics Ltd.  21 Barry's Point Road, AKL, New Zealand
> PH    (649) 489-2280 
> FX    (649) 489-2290
> email [EMAIL PROTECTED]  or  [EMAIL PROTECTED]
> ======================================================================
> 
> --------------------------------------------------------------
> -------------
>     New Zealand Delphi Users group - Delphi List - 
> [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
> 

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to