Custom code for Age calculation | XM Community
Solved

Custom code for Age calculation

  • 17 August 2023
  • 7 replies
  • 107 views

Userlevel 3
Badge +2

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

icon

Best answer by TomG 10 September 2023, 17:14

View original

7 replies

Userlevel 5
Badge +10

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

Userlevel 5
Badge +17

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

 

 

 

Userlevel 7
Badge +27

@yaminaaloui -

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

Userlevel 3
Badge +2

Thank you all!

Userlevel 3
Badge +2

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?

Userlevel 7
Badge +27

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. 

Userlevel 1
Badge +4

@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