Calculating age in months and days

Hi,

Im trying to calculate age in months and days in Commcare. The calculation
will be done as 'current date - DOB'

I know you can do the calculation seperately as months, years, weeks or
days, but is there anyway to set a claculation and show the output as
months and days (e.g. 24 months, 15 days)?

Thanks in advance for any help!

Hi Sabeth,

There is an operator you can use in CommCare logic called "mod". You can
use it to get the remainder of days after you divide to get the number of
months.

We have some documentation here that might be helpful: https://confluence.
dimagi.com/display/commcarepublic/CommCare+Functions#CommCareFunctions-mod

··· On Wed, Oct 12, 2016 at 4:01 AM, Sabeth Ahmed wrote:

Hi,

Im trying to calculate age in months and days in Commcare. The calculation
will be done as 'current date - DOB'

I know you can do the calculation seperately as months, years, weeks or
days, but is there anyway to set a claculation and show the output as
months and days (e.g. 24 months, 15 days)?

Thanks in advance for any help!

--
You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi Amelia,

Thank you very much for this! I will try it out

··· On Oct 12, 2016 9:59 PM, "Amelia Sagoff" wrote:

Hi Sabeth,

There is an operator you can use in CommCare logic called "mod". You can
use it to get the remainder of days after you divide to get the number of
months.

We have some documentation here that might be helpful:
CommCare Functions - CommCare Public - CommCare Public
CommCareFunctions-mod

On Wed, Oct 12, 2016 at 4:01 AM, Sabeth Ahmed smunrat@gmail.com wrote:

Hi,

Im trying to calculate age in months and days in Commcare. The
calculation will be done as 'current date - DOB'

I know you can do the calculation seperately as months, years, weeks or
days, but is there anyway to set a claculation and show the output as
months and days (e.g. 24 months, 15 days)?

Thanks in advance for any help!

--
You received this message because you are subscribed to the Google Groups
"commcare-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the
Google Groups "commcare-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/
topic/commcare-users/MKvy25c2u3o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
commcare-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hi Sabeth_Ahmed,

You’re right—most simple age calculations give you only years, months, weeks, or days separately. To show something like “24 months, 15 days,” you need to do two steps:

  1. Calculate the total number of months between the DOB and today.

  2. Calculate the remaining days after subtracting those months.

So basically:

  • First, find how many full months have passed.

  • Then, take the leftover days after those months and display them together.

In CommCare, you might need to use a combination of date arithmetic functions and formulas to get both values and then join them in one display like:
concat(months, " months, ", days, " days").

This will give the output exactly like “24 months, 15 days.”

You can calculate age in months and days in CommCare by first getting the total months, then subtracting full months to get the remaining days. Example approach:

months = datediff(today(), DOB, "months")
days = datediff(today(), date_add(DOB, months, "months"), "days")

This gives output like “24 months, 15 days.”

Calculating precise ages in CommCare can be a bit of a puzzle, especially when you need to account for varying month lengths and leap years. I’ve found that using a combination of the int((today() - date) div 30.4375) logic for months usually gets you very close for most use cases.

If you need it to be exact for medical or registration purposes, it’s often easier to calculate the total days first and then use a couple of hidden calculations to break that down into months and remaining days. It keeps the logic a bit cleaner and easier to troubleshoot if a calculation looks off in the field.

Great discussion! I can see that several users have already provided helpful approaches to calculating age in months and days. Let me add some additional context from the CommCare documentation to round out the conversation.

Eris_Casper, you're absolutely right that precise age calculations can be tricky! The approaches mentioned in this thread are all valid depending on your specific needs. Here are a few options to consider:

Recommended Approaches:

1. Using date difference functions (Most Accurate)

The approach vipulkpp mentioned is very solid for getting exact months and remaining days:

months = int((today() - DOB) div 30.4375)
days = (today() - DOB) - (months * 30.4375)

However, for even more precision, you could calculate:

  • Months: Use date arithmetic to find complete months
  • Remaining days: Use mod (modulo) function as Amelia suggested to get the remainder

2. Total days approach (What you mentioned)

Your method of calculating total days first and then breaking it down is also quite practical:

total_days = today() - DOB
months = int(total_days div 30.4375)
remaining_days = int(total_days mod 30.4375)

Then display with: concat(string(months), " months, ", string(remaining_days), " days")

Important Considerations:

  1. Month lengths vary - Using 30.4375 (average days per month) gives you a good approximation, but won't be exact for every calendar month
  2. Hidden calculations - As you mentioned, using hidden value questions for intermediate calculations makes debugging much easier
  3. Data type conversions - Remember to use string() function when combining numbers with text in concat()

The exact approach depends on whether you need:

  • Approximate age (for general tracking) → the 30.4375 divisor works well
  • Exact calendar months and days (for medical/legal purposes) → may need more complex date arithmetic

Would you like help implementing a specific calculation for your use case?