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
Post a Comment