## Microsoft Dynamics CRM Forum

Shane Brown asked a question on 15 Aug 2017 8:26 AM

### Question Status

Verified

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

Aric Levin responded on 15 Aug 2017 8:49 AM

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

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.

Alex Shlega responded on 15 Aug 2017 9:15 AM

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

Carl Fazekas responded on 15 Aug 2017 9:30 AM

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 responded on 6 Oct 2017 4:55 PM

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

Carl Fazekas responded on 15 Aug 2017 9:30 AM

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.

Aric Levin responded on 15 Aug 2017 8:49 AM

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

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.

Alex Shlega responded on 15 Aug 2017 9:15 AM

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