Calculate age

how to calculate age in months, days and years
I need your help I am a beginner xlsform

You need to insert a formula in the column labeled calculation. E.g 2019-${year_of_birth} where year_of_birth is the year the respondent was born.

how to do for months and years of births

The following will calculate the age, in days, from their date-of-birth:

calculate = "round(decimal-date-time(today()) - decimal-date-time(${dob}))"

(I round because due to floating point truncation you can sometimes get a difference of, say, 1233.999998 days. If you blindly truncate this you could end up a day off).

Although it seems innocuous to be asking for months and years, in reality the question is ill-posed. Specifically, what precisely is a "year"? Some years can be 365 days, others 366. Similarly, what precisely is a "month"? 28,29,30 or 31 days?

It probably depends on what you plan to use the result for. For example, if you are going to use it to compare two ages together and see who is older, then both ages will need to be computed using the same constant for #days in month and #days in year. But if instead you merely want to display that someone born on, say, January 1, 2018 is now 1 year, 1 month and 14 days old today (its Feb-15-2019 here as I write this), then you'll need to take into account how many days are in the specific intervening months (and years).

2 Likes