Calculate Age In Day, Months and Years in X++

I got required to display exact age of employee in days, month and years. I used some DateTimeUtil method and achieve my output.

 public static void getAge(Args args)  
 {  
   int       years,  
           months;  
   int64      days;  
   utcDateTime   birthDayCurrentYear;  
   str       ageInWords;  
   date    _birthDate  
   ;  
   months = mthOfYr(systemdateget()) - mthOfYr(_birthDate);  
   years = year(systemdateget()) - year(_birthDate);  
   if (dayOfMth(systemdateget()) < dayOfMth(_birthDate))  
   {  
     months--;  
   }  
   if (months < 0)  
   {  
     years--;  
     months += 12;  
   }  
   #timeConstants  
   birthDayCurrentYear = DateTimeUtil::addMonths(DateTimeUtil::newDateTime(_birthDate,0), ((years * 12) + months));  
   days = DateTimeUtil::getDifference(DateTimeUtil::newDateTime(systemdateget(),0) , birthDayCurrentYear) / #secondsPerDay;  
   ageInWords = strFmt("%1 years, %2 months, days %3", years, months, days);  
   info(ageInWords);  
 }  

Comments

Popular posts from this blog

X++ Get Start Date and End Date of Month

Dynamics AX 365 : The name does not denote a class, a table or an extended data type

Error while setting server report parameters. Error message: The permissions granted to user are insufficient for performing this operation. (rsAccessDenied)