Função para calcular a idade.
Esta é um dica simples dica para você calcular a idade.
É necessário estar declarado SysUtils ma seção uses,
em versões unicode declare System.SysUtils.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| function CalcularIdade(Nascimento: TDate): Integer;
var
Month, Day, Year, CurrentYear, CurrentMonth, CurrentDay: Word;
begin
DecodeDate(Nascimento, Year, Month, Day);
DecodeDate(Date, 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; |
function CalcularIdade(Nascimento: TDate): Integer;
var
Month, Day, Year, CurrentYear, CurrentMonth, CurrentDay: Word;
begin
DecodeDate(Nascimento, Year, Month, Day);
DecodeDate(Date, 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;
Exemplo de uso:
1
2
3
4
5
| procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Format('Sua idade é %d.',
[CalcularIdade(StrToDate('10/05/1987') )]));
end; |
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(Format('Sua idade é %d.',
[CalcularIdade(StrToDate('10/05/1987') )]));
end;
-
InfusTec
-
8.672 views
- 0 comentários
- 10 de abril de 2015
Está gostando do conteúdo? Considere pagar um cafezinho para nossa equipe!
Posts Relacionados - Continue Aprendendo