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 :
Microsoft Dynamics CRM (Archived)

Calculating Age based on DOB

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Not sure if you are trying to achieve this in JavaScript or C#, but you can use the following logic:

    var oldDate =new Date(2000, 0, 1); //Month is 0-11 in JavaScript

    today=new Date();

    //Get 1 day in milliseconds

    var one_day=1000*60*60*24

    //Calculate difference btw the two dates, and convert to days

    var todayDays = Math.ceil((today.getTime()-millennium.getTime())/(one_day));

    See following article for more info, but there are plenty more:

    www.javascriptkit.com/.../datedifference.shtml

    In C# it's much easier:

    int totalDays = EndDate.Date.Subtract(DateTime.Now.Date).Days;

    or

    int totalDays = DateTime.Now.Subtract(EndDate.Date).Days;

    Hope this helps.

  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    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

  • Verified answer
    CSF90 Profile Picture
    on at

    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.

  • Shane Brown Profile Picture
    190 on at

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

  • Suggested answer
    Vipin J Profile Picture
    1,603 on at

    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

  • Suggested answer
    Namrata11 Profile Picture
    77 on at

    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 :)

  • Bruna Teixeira Profile Picture
    5 on at

    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.

  • TomSmelser Profile Picture
    1,570 on at

    This worked for me. Thank you.

  • Suggested answer
    Namrata11 Profile Picture
    77 on at

    You can use Date only field

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

  • Kishore P Profile Picture
    45 on at

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

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans