 Mirror

How to calculate the age based on the date of birth (Views: 100)

 Problem/Question/Abstract:How to calculate the age based on the date of birth.Answer:Solve 1:{ BrthDate:  Date of birth }function TFFuncs.CalcAge(brthdate: TDateTime): Integer;var  month, day, year, bmonth, bday, byear: word;begin  DecodeDate(BrthDate, byear, bmonth, bday);  if bmonth = 0 then    result := 0  else  begin    DecodeDate(Date, year, month, day);    result := year - byear;    if (100 * month + day) < (100 * bmonth + bday) then      result := result - 1;  end;end;Solve 2:procedure TForm1.Button1Click(Sender: TObject);var  Month, Day, Year, CurrentMonth, CurrentDay, CurrentYear: word;  Age: integer;begin  DecodeDate(DateTimePicker1.Date, Year, Month, Day);  DecodeDate(Date, CurrentYear, CurrentMonth, CurrentDay);  if (Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then    Age := 0  else  begin    Age := CurrentYear - Year;    if (Month > CurrentMonth) then      dec(Age)    else if Month = CurrentMonth then      if (Day > CurrentDay) then        dec(Age);  end;  Label1.Caption := IntToStr(Age);end;

<< Back to main page