x++dynamics-365dynamics-365-operations

Calculating Age in X++


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

Info Result


Solution

  • You're using round(...) incorrectly.

    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.

    See https://learn.microsoft.com/en-us/dynamics365/fin-ops-core/dev-itpro/dev-ref/xpp-math-run-time-functions