Age in months from integer questions

I am working on a form where DOB is entered in form of three integers, one integer for days, one for months, and one for year. I am aware of the “date” question types, however we need integers.
How can I calculate age in months?
I have been trying to use concat function to form yyyy-m-d from the integers and then subtracting that from today()/ div 30.24). This works very wel but only in Enketo, in doesn’t work in Kobo collect.
Any suggestions?


@pkc1, could you share the relevant part of your XLSForm with the community, please? Maybe the community should be able to help you out.

Caregiver test.xlsx (212.6 KB)
Hello @Kal_Lam ,
here is the extract. The row highlighted in yellow is in question. This works very well in Enketo but not in ODK/Kobocollect. I would need something that would work in Kobocollect.
Many thanks!

1 Like

@pkc1, pinging @Xiphware to see if it’s a Collect Android App issue as it’s working with Enketo.

@pkc1 I used the info in this ODK forum post to adapt your form, it can show months in Kobocollect. You may need to use round() to not show decimals.
Caregiver test.xlsx (182.0 KB)

1 Like

You appear to be using this to generate the date from the component year, month and day integer inputs:

concat(${B04_3_year}, '-',${B04_3_month}, '-',${B04_3_day}

However, you need to be careful here, because strictly speaking the correct format for dates is “YYYY-MM-DD” [see XML Schema Date/Time Datatypes]. Specifically, a month or day value that is less that 10 should be zero padded, eg 2024-04-01, which your above calculation doesn’t

So what you might be seeing is that by a mere quirk of how Enketo’s XForms engine handles date components internally that it is serendipitously not sensitive to this, whereas ODK Collect/KoboCollect’s javaRosa engine probably is.

Have you tried to see if yours works under KoboCollect when both the month and day are double-digit numbers?

BTW, aside but I also noticed this in your form:

(.>=1 and .<=31) or .= 98 and (regex(., '^[01-31]{2}$'))

This is not the correct regex expression if you want to be checking for a range of numbers; see Regex for Numbers and Number Range (With Examples) - Regex Tutorial - Regular Expressions Simplified for examples.

1 Like

FYI, in regards to your original question about calculating age in months (or days, or years for that matter), you might be interested in an article I just posted to the ODK Showcase forum showing on how to reliably calculate the correct age: Calculate age in years, months and/or days - Showcase - ODK Forum