DELPHI FAQ: Как подсчитать возраст по дню рождения?


Как подсчитать возраст по дню рождения?
Previous  Home  Next




{ 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
;






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
;


Взято с Delphi Knowledge Base
http://www.baltsoft.com/







DELPHI FAQ




EOMY TOP 100      Рейтинг@Mail.ru      Rambler's Top100