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

Custom code for Age calculation


Forum|alt.badge.img+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

Best answer by TomG

@yaminaaloui -

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

View original

7 replies

Forum|alt.badge.img+10
  • Level 4 ●●●●
  • 48 replies
  • September 9, 2023
yaminaaloui wrote:

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


Forum|alt.badge.img+25
  • Level 4 ●●●●
  • 61 replies
  • September 9, 2023

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • Answer
  • September 10, 2023

@yaminaaloui -

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


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 7 replies
  • September 11, 2023

Thank you all!


Forum|alt.badge.img+2
  • Author
  • Level 2 ●●
  • 7 replies
  • September 12, 2023

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?


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5926 replies
  • September 12, 2023
yaminaaloui wrote:

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. 


Adam_Jermain
Level 2 ●●
Forum|alt.badge.img+4
  • Level 2 ●●
  • 9 replies
  • May 9, 2024
TomG wrote:

@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