Calculate Age Person by Birthdate on Delphi

Hello Now instant make calculate age person by birthdate, date mode we use dd/mm/yyyy or dd.mm.yyyy :

function CalculateAge(Birthday, CurrentDate: TDate): Integer; 
var 
  Month, Day, Year, CurrentYear, CurrentMonth, CurrentDay: Word; 
begin 
  DecodeDate(Birthday, Year, Month, Day); 
  DecodeDate(CurrentDate, CurrentYear, CurrentMonth, CurrentDay); 
  if (Year = CurrentYear) and (Month = CurrentMonth) and (Day = CurrentDay) then 
  begin 
    Result := 0; 
  end 
  else 
  begin 
    Result := CurrentYear - Year; 
    if (Month > CurrentMonth) then 
      Dec(Result) 
    else 
    begin 
      if Month = CurrentMonth then 
        if (Day > CurrentDay) then 
          Dec(Result); 
    end; 
  end; 
end;

and for use it


label1.Caption := Format('Age is %d', [CalculateAge(StrToDate('String format date exp: 27.08.2009'), Date)])

0 Response to "Calculate Age Person by Birthdate on Delphi"

Post a Comment