Calculation Age from DOB

Hello All,

we have problem in age calculation form Date of Birth to Current Day issue.
I use the following function from commcare wiki. But it was calculated in next year only.
we would like to seen the register age is 35 , so next year is 36.

May i know and explain about this problems please.

`int((today() - date(#form/dob)) div 365.25)

Best regards,
Lin

Hi @Lin_Htet1, can you please expand more on your question or if possible share a screenshot .

1 Like

There are a few other ways of calculating age in years on the wiki - the expression you pasted in is an estimate that can be off by 1 if it's near the birthday and the leap years don't line up well. For a precise age in years, you can use this expression:

if(format-date(today(), "%m%d") >= format-date(#form/dob, "%m%d"),
   format-date(today(), "%Y") - format-date(#form/dob, "%Y"),
   format-date(today(), "%Y") - format-date(#form/dob, "%Y") - 1)

I'm not sure what you mean by "it was calculated in next year only". Do you mean that the calculation was incorrect? Could you expand more, as Funmito suggested?

1 Like

Hello Funmito and Ethan Soergel,
Thanks you very much of comments and
Meaning that , we calculate the Age. When the person register date is 25 years old. The person comes back after 3 month or 6 months and 10 days ( May Be).
We would like to calculate and show with labels. Just we show the label but also don't correct the Age in current day, month and years. The Age will be calculated after 1 year.

Best regards,
Lin

You can make a hidden value with the calculation and display that in a label. There's no need to save it to the case.

It sound like you're trying to calculate the person's age at a future date. That should be straightforward as well. You'll need the exact date and the person's date of birth. If you have the future visit date in a question already, you can use that, or you can calculate it in a hidden value. Then you can use the same expression to calculate the person's age, but wherever it says today(), instead use a reference to the future visit date:

if(format-date(#form/next_visit_date, "%m%d") >= format-date(#form/dob, "%m%d"),
   format-date(#form/next_visit_date, "%Y") - format-date(#form/dob, "%Y"),
   format-date(#form/next_visit_date, "%Y") - format-date(#form/dob, "%Y") - 1)