[英]Calculating Age in X++
我正在嘗試計算 x++ 中的年齡,其中客戶出生於 2010 年 1 月 6 日到他訪問的選定日期 - 今天 2023 年 1 月 6 日,但結果並沒有給我 13 歲,而是給了我 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));
由於閏年,我嘗試使用 / 365 和 365.25 但仍然效果不佳
您錯誤地使用round(...)
。
ageDiffInt = decRound(ageDiffReal, 0); // Better
ageDiffInt = round(ageDiffReal, 1); // Works too
round(...)
- 是 _decimals 參數指定值的倍數並且最接近 _arg 參數指定值的數字。
所以這是我找到的完美工作的解決方案。
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.