Calculate Birthday - Age and Days

TFCrowtherTFCrowther Member Posts: 35
edited 2006-02-20 in NAV Tips & Tricks
Enter in your birthday into a form field name "Birth Date" and it will return
Agrstr = Your age.
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

Comments

  • wicwic Member Posts: 96
    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 ######
  • Slawek_GuzekSlawek_Guzek Member Posts: 1,690
    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;
    PRINCE2 Practitioner - License GR657010572SG
    GDPR Certified Data Protection Officer - PECB License DPCDPO1025070-2018-03
  • SuDSuD 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);

Sign In or Register to comment.