Age in Years

Hi All,

I have 2 issues that am coming across scripting in xlsform
1:Am trying to come up with estimated year based on year of birth eg if respondent is not sure of a hh member age then we take year of birth and add 30 years ie 1991+30 but as it is am getting very strange figures with ${Birth}+30 ie 108639.125 etc not the year expected but if the respondent knows the exact age then it shouldnt calculate but let the respondent state the age
Note that I have question to indicate whether the age is exact(thus respondent gives exact age) or estimated/not sure(calculations takes effect)

2:How can I set default to be if respondent is male then the next question default will be Female but if responent is Female then next question default is male.


Age in Years

Did you take a look at this thread: Calculate age

Hi Xiphware,

Yes,I have.
I tried it but it just rounding off and not giving the years eg 1971+30 to have 2001 as the year.


Could you perhaps describe (or post your form) showing how you are calculating ${Birth}. It’s not quite clear if it’s a date field, year (integer) field, or decimal date/days...

Am using a date field but year appearance.

age sample.xlsx (8.6 KB)

K. So your ${Birth} is type date, which is actually stored as a string of the form 'YYYY-MM-DD'. The best way to extract the year from a date is using the format-date() function; specifically in your case


This will give you the year as a number, which you can then use in any numeric calculation to your heart's desire... :slight_smile:

(here's a form to play around with)

datetest.xls (5.5 KB)

BTW, in your original form you were basically try to add a number to a string; ie

'2019-05-17' + 30

which is obviously, um, problematic... :wink: I think the mistake you made is assuming the 'year' appearance would give you just the year; it doesnt - it simply gives you are regular date of the form 'YYYY-01-01'. So you still have to extract the year from a date using format-date()


:grin: :joy:
I read that partcular thread with format-date(${birth},'%Y') as an example but for some reason became fixated with the ${Birth}+n from a different thread thus thought it was a shorter version.
It worked as expected.