Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Calculating Age based on DOB

Posted on by 190

Hi 

I created a Calculated field using Diffindays to calculate Age based on Today and dob field.

'DiffInDays(cat_dateofbirth, Now()) / 365'

 

The issue I have it does not take into consideration leap years. Is there anything I could add to the formula to make this work?

Or any code that does the job?

Thanks

 

 

*This post is locked for comments

  • RE: Calculating Age based on DOB

    Hi Family ,

    Please Do Refer Blog of Mine

    I hope Your Question will be Answered

    dynamics365starsolutions.blogspot.com/.../age-using-javascript-in-d365-to-get-age.html

  • Kishore P Profile Picture
    Kishore P 45 on at
    RE: Calculating Age based on DOB

    Hi ,

    Try these:

    var DOB =Xrm.Page.getAttribute("DOB").getValue();
    var today = new Date();
    var birthDate = new Date(DOB);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
    age = age - 1;
    }
    Xrm.Page.getAttribute("new_Age").setValue(age);
  • Suggested answer
    Namrata11 Profile Picture
    Namrata11 75 on at
    RE: Calculating Age based on DOB

    You can use Date only field

    It is about Date of birth, we can consider as date only field

  • TomSmelser Profile Picture
    TomSmelser 1,570 on at
    RE: Calculating Age based on DOB

    This worked for me. Thank you.

  • Bruna Teixeira Profile Picture
    Bruna Teixeira 5 on at
    RE: Calculating Age based on DOB

    Hi Namrata,

    I've taken a look at your suggestion and I think the problem is to create a new DOB field using User Local behavior. It could be a problem for users in different time zones as the date can change.

    I agree with the guys, I think there is no OOB way to solve this.

  • Suggested answer
    Namrata11 Profile Picture
    Namrata11 75 on at
    RE: Calculating Age based on DOB

    Hi,

    I have calculated age using OOB Calculated field.

    https://namratapani.wordpress.com/2019/03/20/oob-age-calculation-in-crm/ 

    Please check this once. Worked for me :)

  • Suggested answer
    Vipin J Profile Picture
    Vipin J 1,583 on at
    RE: Calculating Age based on DOB

    The business rule where seems to be not working ang giving an error - You can't use Now(), which is of type dateTime, with the current function.

    So i wrote a JavaScript to support my calculation of Age. Click Here

    vjcity.blogspot.com/.../how-to-calculate-age-in-dynamic-365-crm.html

  • Shane Brown Profile Picture
    Shane Brown 190 on at
    RE: Calculating Age based on DOB

    Agreed doesn’t look like it is possible with OOB functionality.

  • Verified answer
    CSF90 Profile Picture
    CSF90 on at
    RE: Calculating Age based on DOB

    This is the classic example of a solution that sounds like it should be simple but is actually really complex. You'll need a JavaScript function for it to be accurate - there is no way calculated fields or business rules will be able to work it out for you.

  • Suggested answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: Calculating Age based on DOB

    Hi Shane,

     why don't you use DiffinYears instead of DiffinDays?

     Or are you trying to get a decimal number for the age (as in 10.5, 40.73)?

     If that's the case, you might probably try:

    DiffinYears(cat_dateofbirth, NOW()) +DiffinDays(ADDYEARS(cat_dateofbirth, DiffinYears(cat_dateofbirth, NOW())), NOW()) / 365

     In other words, first get diff in years.. then add the portion of the year which is left after that

     There could still be a bit of difference, but it'would only be there if the last year was a leap year

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans