Author: Jonas Bilinkevicius
How to calculate the age based on the date of birth.
Answer:
Solve 1:
1 { BrthDate: Date of birth }
2
3 function TFFuncs.CalcAge(brthdate: TDateTime): Integer;
4 var
5 month, day, year, bmonth, bday, byear: word;
6 begin
7 DecodeDate(BrthDate, byear, bmonth, bday);
8 if bmonth = 0 then
9 result := 0
10 else
11 begin
12 DecodeDate(Date, year, month, day);
13 result := year - byear;
14 if (100 * month + day) < (100 * bmonth + bday) then
15 result := result - 1;
16 end;
17 end;
Solve 2:
18
19 procedure TForm1.Button1Click(Sender: TObject);
20 var
21 Month, Day, Year, CurrentMonth, CurrentDay, CurrentYear: word;
22 Age: integer;
23 begin
24 DecodeDate(DateTimePicker1.Date, Year, Month, Day);
25 DecodeDate(Date, CurrentYear, CurrentMonth, CurrentDay);
26 if (Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then
27 Age := 0
28 else
29 begin
30 Age := CurrentYear - Year;
31 if (Month > CurrentMonth) then
32 dec(Age)
33 else if Month = CurrentMonth then
34 if (Day > CurrentDay) then
35 dec(Age);
36 end;
37 Label1.Caption := IntToStr(Age);
38 end;
|