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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Display Age in Years - Months - days

(0) ShareShare
ReportReport
Posted on by 634

Hi Experts,

I have a custom asset table where I have acquisition date for each asset. I need to display the age of assets in Year- month - days format (Example:- 1 Year 2 months 5 days) in the form, What is the best way to get this done. Instead of a display field if user can sort the records by age that would be great. Any suggestions please. 

Currently the age is being displayed in the form in months using the below. 

Display int NoOfMonths()
{
if(this.DateAcquisition)
{
return intvNo(today(),this.DateAcquisition,IntvScale::YearMonth);

}
else
{
return 0;
}
}

Thanks in advance. 

I have the same question (0)
  • Suggested answer
    Ludwig Reinhard Profile Picture
    Microsoft Employee on at

    Hi AX beginner,

    What's the use case for this requirement?

    Is it just that management wants to know how old an asset is?

    If so, why do you need the days?

    I would assume that the year and month should be sufficient.

    Would be great if you could elaborate.

    Many thanks,

    Ludwig

  • D365  beginner Profile Picture
    634 on at

    Thanks Mr. Ludwig for the response.

    Yes, The management just need to see the asset age in Years and Months in the form. No need of days actually, but for the sake of curiosity I was asking if it is possible to do or not.

  • D365  beginner Profile Picture
    634 on at

    Any help would be highly appreciated. 

  • Suggested answer
    MCallebert Profile Picture
    45 on at

    Hi,

    you can try something like this :

    static void Job149(Args _args)
    {
        int             years, months, days;
        utcDateTime     yourUtcDateTime;  
        date            yourDate;
        
        yourDate = mkDate(17,11,2020);
       
        months = mthOfYr(systemdateget()) - mthOfYr(yourDate);
        years = year(systemdateget()) - year(yourDate);
        
        if (dayOfMth(systemdateget()) < dayOfMth(yourDate))
        {
             months--;
        }
        
        if (months < 0)
        {
            years--;
            months  = 12;
        }
        
        #timeConstants
        yourUtcDateTime = DateTimeUtil::addMonths(DateTimeUtil::newDateTime(yourDate,0), ((years * 12)   months));
        days = DateTimeUtil::getDifference(DateTimeUtil::newDateTime(systemdateget(),0) , yourUtcDateTime) / #secondsPerDay;
        info(strFmt("%1 years %2 months %3 days", years, months, days));
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans