Solved

# Calculate age based on date of birth.

Posted on 2004-11-05
933 Views
Hi,

In a tabel I want to calculate the age of people based on the date of birth.

Today it is the 5th of november 2004.
When the date of birth is the 6th of november 1965, the age should be 38.
But it is calculated as 39.

procedure TDM.TRelatiesCalcFields(DataSet: TDataSet);
begin
If DateToStr(DM.TRelaties.FieldByName('Geboortedatum').AsDateTime)<>'30-12-1899' then
DM.TRelaties.FieldByName('Leeftijd').asInteger:=YearsBetween(Now,DM.TRelaties.FieldbyName('Geboortedatum').asdatetime)
else
DM.TRelaties.FieldByName('Leeftijd').asInteger:=0;
end;

Can anybody finetune my calculation?

Thanks Stef
0
Question by:Delphiwizard

LVL 17

Accepted Solution

Hi,
Try
YearsBetween(DateOf(Now),DateOf(DM.TRelaties.FieldbyName('Geboortedatum').asdatetime))
0

LVL 31

Expert Comment

The following depends on how you populate your table, and whether you are using the BDE.

You don't have the Regional settings set for the wrong country perchance?  (Or ditto with the BDE, but not both)

If this is the UK (with US date format) then you would get:-

11 May 2004
11 June 1965

which is 39 complete years.

0

LVL 13

Expert Comment

works ok on my machine... maybe stripping off the time as mokule suggested

YearsBetween(DateOf(Now),DateOf(DM.TRelaties.FieldbyName('Geboortedatum').asdatetime))
0

## Featured Post

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

#### Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!