web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Age calculation using x++

(0) ShareShare
ReportReport
Posted on by 2,426

Good Morning,

I am trying to calculate age.

i am comparing from today date to given birth date
Exampl - year(Today()) - year(mkdate(1,1,1998))
But i am getting 30
it must be 29
How can i achieve this.
I have seen in some sites
But using that intv function i am getting negative values.
Please show some light on this.
Regards.
Have a great day.

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    239,257 Most Valuable Professional on at

    2018-1998 clearly is 20, therefore your code doesn't match your requirements.

    You should think again how you define age. For example, you may want to round age to the closest year, e.g. age 20.4 would be rounded to 20. Start writing code only when you know what calculation you want to implement.

  • AX 2012 r3 Profile Picture
    2,426 on at

    Hi Martin,

    Thanks for your reply.

    Calculator.png,

    How can we achieve this.

    Regards.

    Have a great day.

  • Suggested answer
    dekaasboer Profile Picture
    1,530 on at

    2018 - 1998 = 20

  • Suggested answer
    Rudi Hansen Profile Picture
    4,079 on at

    Except for the fact that 2018 - 1998 is not 30 but 20, that's not really your problem.

    The problem is that you are rounding your numbers too early.

    static void Job9(Args _args)
    {
        real ageDiffReal;
        int  ageDiffInt;
    
        ;
        ageDiffReal = year(Today()) - year(mkdate(1,1,1998));  // Since you take the year of the dates and subtract you will get the wrong result (2018-1998 = 20)
        info(strFmt('%1',ageDiffReal));
        
        ageDiffReal = (Today() - mkDate(30,6,1998)) / 365; // This is the correct way to do it
        ageDiffInt  = Round(ageDiffReal,0); // And when round down the number.
        
        info(strFmt('%1,%2',ageDiffReal, ageDiffInt));
    
    }


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
CP04-islander Profile Picture

CP04-islander 26

#2
imran ul haq Profile Picture

imran ul haq 8

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 4 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans