Auto generating age in years and months from Date of Birth

Sorry I didn’t understand what you meant exactly:
But in my work we are using ID entering for beneficiaries to avoid duplicating in names … to match assessment data with distribution data:
ID number is unique for all citizens and consists from 11 numbers… so in above regex i force enumerators to add only number with “text” type of question… we have 2 languages Arabic and English… Some times they are laptops to fill data into server

Unfortunately, multi-lingual regexs are beyond my scope of expertise (I don’t even know if you can do them). Perhaps someone else here who has successfully implemented constraint checking against non-English text input can provide some insight?

Yes Dear we are using Arabic Language mainly but the form in English as you know, So to prevent people making mistake I did that regex.

Dear @stephanealoo,

I have the exact same question as asked by @Collins. I am having a hard time in understanding your code - where do I paste it in my Excel form?

I have two date variables : 1) date of survey and 2) date of birth

Would really appreciate if you could help me understand this.

Thanking in anticipation.

Hi @mahrukhkhan

That was such a long time. There is now an article that clearly describes how to do advanced calculation. Kindly check on it here and if you don’t find your solution, please type your question and example so that we can chime in on it.

Regards
Stephane

1 Like

Hi @stephanealoo,

Actually my question is not just about the calculation of dates. It is about the display of the calculation in Years, Months.

I have a date variable which opens up the calendar to enter date of survey.
I have another date variable which opens up the calendar to enter the date of birth.

I then calculate the age by subtracting the date of birth from the date of survey. In my data set, if the child is 15 months old, the age displayed is “15 months”. However, I want the display to be “1 year 3 months”.

Is it possible to do this on Kobo?

Please let me know if there is a solution to this.

Hi
I created a form just to explain this find it attached hereCalculateExamples.xls (26 KB)

Focus on the area shown below

Your output should be something like this

And as indicated by @Xiphware FYI depending on how pedantic you wish to be, you might be interested in this thread (specifically discussing leap years…). But since you just want years and months here - that is, the precise number of days doesn’t matter - the impact of leap years will be nominal.
Regards
Stephane

2 Likes

That worked. Thank you so much @stephanealoo! Really appreciate your effort.

2 Likes

FYI depending on how pedantic you wish to be, you might be interested in this thread (specifically discussing leap years…). But since you just want years and months here - that is, the precise number of days doesn’t matter - the impact of leap years will be nominal.

2 Likes

Hi @Xiphware,

Thanks for the link. This is super useful!

1 Like

@Xiphware adding this component to the solution

1 Like

Dear @stephanealoo, about age calculation, how does one do it directly from the KoBoToobox form generation to get this kind 0f form preview as per your post. The article on Advanced Use of Calculate Questions does not seem to explain well.

Hi,
You should use the same principle used in the previous form, the only difference would be that you are adding this on the form builder as opposed to an XLS form.

  • You will need to create a calculate question from the form builder
    image

  • You will then have to type your formula in the bar. Remember to use the reference to the previous questions that have the date of birth and date of interview. Please note that the formula will be the same as to the one you could have used in XLS

You will simply type the following into the calculate (${interviewdate}-${birth}) div 365

image

Unfortunately when working with FormBuilder you can not create calcuations on other types of questions such as text or integer, you must create it on calculate questions. When viewing the form, calculate questions will not be shown.

Stephane

2 Likes

Received with thanks @stephanealoo. Will keep you posted on how it goes.

1 Like

Hi stephanealoo, thanks very much for helping with the auto age calculation. I have prepared a short version of this for testing and it works.

1 Like

@aahmed
I am glad that this was still helpful one year down the line :grinning:

Stephane

2 Likes

Hi
Find a shorter solution

date dob What is your Date of Birth?
calculate age_years Age in Years int((today() - ${dob}) div 365.25)
calculate age_mnths_remainder Age in Months Remainder int((today() - ${dob}) div 30.44) mod 12
note age ${full_name} is ${age_years} years and ${age_mnths_remainder} months old
1 Like

Thank you for sharing this wonderful solution with the entire community, @tba! :clap: Expecting the same in the upcoming days too. :pray:

Hello @stephanealoo in your example is it possible to calculate age by using current date(Meta data) instead of interview date.

I want end user to provide me only date of birth in date format then in the backend I want to subtract date of birth from today’s date. Can you provide me the right formula for this ? many thanks!

Hello @anilgnydn,
Using today() function together with the birthday will give you the (dynamic) age of today. Which might be different of this person’s age tomorrow already (or the one of a tomorrow submitted person with the same birthday) in your backend. But NOT the age at interview.
Do you really want this?
Also be aware that metadata ${today} acts differently to today() function.

1 Like