#### Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

# Calculate Birthday - Age and Days

Member Posts: 35
edited 2006-02-20
Enter in your birthday into a form field name "Birth Date" and it will return
Age1 = Days since your last birthday.
```“Birth Date” - OnValidate
IF "Birth Date" <>0D THEN BEGIN
Age:= TODAY -"Birth Date"  ; //Returns number of days old

Age2:=(Age/365.2364);  //Returns number of years old as Decimal - Takes into Account Leap Years

i:= STRPOS(FORMAT(Age2),'.');
IF i>0 THEN
Agestr:= COPYSTR(FORMAT(Age2),1,i-1)  //Return no of years old as string – removes decimal value
ELSE
Agestr:= FORMAT(Age2);  //Return no of years old as string

LastBDayTxt := '+' + AgeStr + 'Y';     //Creates a date formula ie +30Y
LastBDayDate := CALCDATE(LastBDayTxt,"Birth Date");   //Returns last Birthday

Age1:= (TODAY - LastBDay);  //Returns number of Days since last birthday
END;
```

Toby Crowther

• Member Posts: 95
hi,
I've a problem with this formula:
TODAY: 4.09.2018 (september)
Birth: 3.09.2008 => age: 10 ok!
Birth: 4.09.2008 => age: 9 (9.999...) not ok, it's birthday!
Birth 5.09.2008 => age: 9 ok!

any idea? something with the 365.2364?

BR
Chris
#### Only one can survive ######
• Member Posts: 1,688
I'd take a bit different approach:
```CheckAgeAndBirthday(BirthdayDate : Date;CheckAgeDate : Date;VAR AgeYears : Integer;VAR AgeDays : Integer;VAR IsBirthDayDate : Boolean)

AgeYears := 0;
REPEAT
AgeYears +=1;
TestDate := CALCDATE( STRSUBSTNO('<+%1Y>', AgeYears), BirthdayDate);
UNTIL TestDate >= CheckAgeDate;

AgeYears -= 1;
AgeDays :=  CheckAgeDate - BirthdayDate;
IsBirthDayDate := TestDate = CheckAgeDate;

//for test only:
MESSAGE('Birthday: %1, Check Date: %2, AgeYears: %3, AgeDays:%4, Is Birthday: %5',
BirthdayDate, CheckAgeDate, AgeYears, AgeDays, IsBirthDayDate);
```

Check dates:
```CheckAgeAndBirthday(030908D, 040918D, AgeYears, AgeDays, IsBirthDayDate);
CheckAgeAndBirthday(040908D, 040918D, AgeYears, AgeDays, IsBirthDayDate);
CheckAgeAndBirthday(050908D, 040918D, AgeYears, AgeDays, IsBirthDayDate);
```

Slawek Guzek
Dynamics NAV, MS SQL Server, Wherescape RED;
GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
• Member Posts: 102
I'd take a bit different approach:
```CheckAgeAndBirthday(BirthdayDate : Date;CheckAgeDate : Date;VAR AgeYears : Integer;VAR AgeDays : Integer;VAR IsBirthDayDate : Boolean)

AgeYears := 0;
REPEAT
AgeYears +=1;
TestDate := CALCDATE( STRSUBSTNO('<+%1Y>', AgeYears), BirthdayDate);
UNTIL TestDate >= CheckAgeDate;

AgeYears -= 1;
AgeDays :=  CheckAgeDate - BirthdayDate;
IsBirthDayDate := TestDate = CheckAgeDate;

//for test only:
MESSAGE('Birthday: %1, Check Date: %2, AgeYears: %3, AgeDays:%4, Is Birthday: %5',
BirthdayDate, CheckAgeDate, AgeYears, AgeDays, IsBirthDayDate);
```

Check dates:
```CheckAgeAndBirthday(030908D, 040918D, AgeYears, AgeDays, IsBirthDayDate);
CheckAgeAndBirthday(040908D, 040918D, AgeYears, AgeDays, IsBirthDayDate);
CheckAgeAndBirthday(050908D, 040918D, AgeYears, AgeDays, IsBirthDayDate);
```

Hi @Slawek_Guzek,

Will it work in the below case?

CheckAgeAndBirthday(210991D, 210992D, AgeYears, AgeDays, IsBirthDayDate);