Skip to main content

Notifications

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 592

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. 

  • Suggested answer
    MCallebert Profile Picture
    45 on at
    RE: Display Age in Years - Months - days

    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));
    }

  • D365  beginner Profile Picture
    592 on at
    RE: Display Age in Years - Months - days

    Any help would be highly appreciated. 

  • D365  beginner Profile Picture
    592 on at
    RE: Display Age in Years - Months - days

    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.

  • Suggested answer
    Ludwig Reinhard Profile Picture
    Microsoft Employee on at
    RE: Display Age in Years - Months - days

    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

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,233 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,982 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans