Custom code for Age calculation | XM Community
Skip to main content

Hi, 

We currently ask for the participant’s DOB but only the month and year in that order.

Is there any way to calculate their age on this basis with a custom code?

Thank you!

Yamina

Hi, 

We currently ask for the participant’s DOB but only the month and year in that order.

Is there any way to calculate their age on this basis with a custom code?

Thank you!

Yamina

You can refer to this post


@yaminaaloui You can refer the below link. Hope this will help!

 

 

 


@yaminaaloui -

Rather than wade through those old threads, you can find the solution here.


Thank you all!


I was wondering if also there is some code to make sure this updates yearly? or is this something that needs to be done manually?


I was wondering if also there is some code to make sure this updates yearly? or is this something that needs to be done manually?

The age is calculated based on the difference between the current date and DOB, so the calculation always remains the same.

You could store DOB in a respondent’s contact record, then calculate age in surveys going forward without asking for DOB. You would use the embedded DOB field from the contact record in place of the DOB text entry question. 


@yaminaaloui -

Rather than wade through those old threads, you can find the solution here.

 

@TomG 

I’ve tried to implement this on a survey in development and cannot get age to calculate. I confirmed that the luxon snippet has been added to the header (via LNF), the code matches as supplied, and we reference e://Field/__js_age for display/branching.

 

However - I’m using the simple layout, which I understand can cause issues with jQuery; I also added 

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js">

...to the header, but no luck.

 

What else might be causing the JS not to run/calculate properly? TIA! 


Leave a Reply