Calculate Age in ODK

Hi again,

I use ODK and a I have a field date with the birthdate, and the ideai is a calculate the age, I use this formula:

  • format-date(today(), ‘%Y’) - format-date(${Birthdate}, ‘%Y’)

And it work, but I need to use all the date, I mean calculate the age with the year, the month and the day.

Can someone help me, please.

Use this calculation to get the age - int((today()-${birthdate}) div 365.25,0)

1 Like

Thanks @ARIF_AZAD_KHAN, it work !!! I really apresiate your help.

on which field does the formula place? Could you give an example? I'm a rookie and I'm about the build.odk

will it run on the xls form

Hi @Daniel_Alberto_Diaz and @Health_screening_19,

It is possible to add calculations in ODK Build, it's in the Advanced options.
Screen Shot 2020-01-30 at 3.06.18 PM
But I think it is easier to use XLSForm. The docs have an example of a calculate type question.

Screen Shot 2020-01-30 at 3.09.09 PM

hi @danbjoseph think this is better. Thanks for the help, I hope someone serves my tip.

edad.xlsx (9.0 KB)

EDIT: Please also see the links later in the thread for additional discussion.

Here is a working XLSForm example:
example_edad.xlsx (9.0 KB)

You can test it at https://opendatakit.org/xlsform/

survey worksheet of attached exampe:

+-----------+------------------+---------------------+----------+---------------------------------------------------+
|   type    |       name       |        label        | required |                    calculation                    |
+-----------+------------------+---------------------+----------+---------------------------------------------------+
| date      | fecha_nacimiento | Fecha de Nacimiento | true     |                                                   |
| calculate | edad             |                     |          | int((today()-${fecha_nacimiento}) div 365.25)     |
| note      | show_age         | Edad: ${edad}       |          |                                                   |
+-----------+------------------+---------------------+----------+---------------------------------------------------+

in Enketo:

1 Like

minor point, but strictly speaking you probably want int(...) not round(...) if you are trying to determine age, in years... [unless of course yer a 20-year old trying to order a drink at the bar in the US... :grin:]

1 Like

:man_facepalming:
thank you. good catch. i've edited my post (and the original answer as well) to reflect that (in case someone doesn't read the full thread).

Thanks, this is great!

Please note there may be some times when the above calculation doesn't work.
See this post:

2 Likes

Perfect this worked fine for me. thanks @danbjoseph

2 Likes

do you the formula if you want to count the months instead of years?

Just FYI this is actually somewhat a under-constrained (but certainly re-occurring!) question. See calculate age for why... :wink: