I am trying to calculate an age in x++ where the customer is born on 1/6/2010 to the selected day of his visit - 1/6/2023 today but the result doesn't give me 13 years old but gives me 12.
real ageDiffReal;
int ageDiffInt;
date datetoday = DateTimeUtil::date(Visitas.RFC_DataVisita);
ageDiffReal = (datetoday - mkDate(dir.BirthDay,dir.BirthMonth,dir.BirthYear)) / 365.242199;
ageDiffInt = Round(ageDiffReal,0);
info(strFmt('%1,%2',ageDiffReal, ageDiffInt));
I tried with / 365 and with 365.25 because of leap years but still didn't work well
You're using round(...)
incorrectly.
ageDiffInt = decRound(ageDiffReal, 0); // Better
ageDiffInt = round(ageDiffReal, 1); // Works too
round(...)
- The number that is a multiple of the value specified by the _decimals parameter and is closest to the value specified by the _arg parameter.
So this was the solution I found in order to work perfectly.
int age;
Date birthDate = mkDate(dir.BirthDay,dir.BirthMonth,dir.BirthYear); //gets the day,month,year from DirPerson table and creates a date.
Date currentDate;
currentDate = DateTimeUtil::date(datavisita);
age = (currentDate - birthDate) / 365;
if (mthOfYr(currentDate) < dir.BirthMonth || (mthOfYr(currentDate) == dir.BirthMonth && dayOfMth(currentDate) < dir.BirthDay))
{
age--; // checks the month and the day and compares it to the person anniversary and if true decrease it by 1
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.