[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 936
  • Last Modified:

Calculate age based on date of birth.

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
Stef Merlijn
Asked:
Stef Merlijn
1 Solution
 
mokuleCommented:
Hi,
Try
YearsBetween(DateOf(Now),DateOf(DM.TRelaties.FieldbyName('Geboortedatum').asdatetime))
0
 
moorhouselondonCommented:
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
 
BlackTigerXCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now