Use embedded data to calculate age on a specific future date, then pipe the result (age) into another question | XM Community
Skip to main content

In my survey, I am asking applicants for their date of birth (DD/MM/YYYY). I would then like to calculate the applicant’s age based on the date of birth they entered in the survey and based on two specific dates in the future (19/02/2024 and 24/07/2024). 

I have created an embedded data field with the field name ‘Age’ and the value is set to the text entry question for the date of birth (${q://QID2/ChoiceTextEntryValue/4}).

 

When the applicant enters their date of birth, I would like to calculate how old they will be on either 19/2/2024 or 24/07/2024. I think I need to use Javascript for this background calculation, but which script do I use for this, there are so many out there and none of them seem to work. Where do I then place the Java script? Into question QID2?

I then want to add branching based on the result of the calculation. If the calcuated age<18 on 19/02/2024 or 24/07/2024, the branching goes one way; if the age is=>18 the branching goes a different way. I have set this up like this:

 

There is so much information out that I am getting slightly lost with the exact steps I need to undertake to achieve this goal. Would someone be able to help me and look into what I might be doing wrong?Layman’s terms would be best for me, as my JS skills are not very good.

Thanks you so much in advance!

@KatjaT 
Check this thread out, it maybe relevant
Javascript to calculate user's age, based on DOB, check age eligibility, & provide validation alert | XM Community (qualtrics.com)

 


Thank you, that was one of the threads I dived into quite deeply, but couldn’t get it to work in my survey.

This is what I have entered in the question HTML:

This is the JS I have attached to that question. I think there could be something wrong with the highlighted line of the script? But I’m not entirely sure...

This is the survey flow I have set up with the embedded data field named age. The way I had hoped to make it happen was:

Applicant enters date of birth in QID2 

JS calculates how old the applicant will be on 19/2/2024 based on their date of birth entered.

If the age is <18, the applicant will be branched off after the ‘Deposit Payment’ block.

I’m not able to get the branching off to work with these scrips, and it is also not yet dependant on the future dates I need to specify. What am I missing here?


@KatjaT,

See this.

Notes:

  1. Moment is no longer being maintained. You should use Luxon instead.
  2. You should load Luxon (in place of Moment) in the survey header or footer, not in the question text.
  3. You should add the age embedded data field with no assigned value before the block containing your DOB question. The name should begin with __js_ (e.g., __js_age).
  4. You should add the JS to your DOB question.
  5. Modify the date format as needed (e.g., dd/MM/yyyy).
  6. You can calculate age at a different date by changing now() to a specific date (i.e,., use fromFormat()).

Hi @TomG 

Thank you! I have followed your steps outlined, but it’s still not branching off successfully.

I have added Luxon in the survey header and removed Moment from the HTML in the question.

I have moved the embedded data container in the survey flow and renamed it to _js_age with no value assigned. The embedded data container is now right before the DOB question.

I have then also edited the branching condition for the age.

I have added this JS to the DOB question:

When I now take the survey and enter that my date of bith is 31/12/2015, meaning I would be 8 years old on 19 February 2024, the survey should branch off to the <18 section. Just not happening for me...


@KatjaT,

There are three issues with your JS code:

  1. The code should be in addOnPageSubmit(), not addOnUnload().
  2. The birthDate fromFormat format should be dd/MM/yyyy
  3. The age fromFormat should be:
fromFormat("19/02/2024","dd/MM/yyyy")

 


This is great @TomG , thank you, I got it to work now with one date. I had to put the DOB question into a seperate question to make it work; previously it was among other form fields and it didn’t work. It also seems like the form field must be named ‘Birth Date’ and can’t be Date of Birth. I tried to adjust the code and changed birthDate to DateofBirth, but there are probably some other issues with this. Either way, it works now with one date, so thank you very much!

Now I do actually need this to work with two different dates/scenarios:

  1. __js_age is less than 18 on 19/02/2024 and the selected intake is ‘Semester 1’, branch off OR
  2. __js_age is less than 18 on 24/07/2024 and the selected intake is ‘Semester2’, branch off

I was thinking I should have two embedded data fields: ‘__js_age’ for the 19/02/2024 and ‘__js_age2’ for the 24/07/2024.

Then add the JS twice, but with the different dates.

Lastly, edit the branching conditions.

 

This seems to break the branching again, and it does not work. Would there be a better solution of how I can achieve this?


You should calculate and set both ages in one function: 

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").val(),"dd/MM/yyyy");
var age = DateTime.dateFrom("19/02/2024","dd/MM/yyyy").diff(birthDate, 'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age',age.years);
var age2 = DateTime.dateFrom("24/07/2024","dd/MM/yyyy").diff(birthDate, 'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age2',age2.years);
});

You can put birthDate in a form. You just have to change the jQuery selector in birthDate to get the value of the correct field. For example, if birthDate was the third field in the form:

var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").eq(2).val(),"dd/MM/yyyy");

In the future, you should post code as I did above instead of as images. Click … in the upper right and select Code.


In the future, you should post code as I did above instead of as images. Click … in the upper right and select Code.

Whoops, sorry, I didn’t even know about this. Will do.

I have done everything exactly as per your comment, but I must still be doing something wrong.

This is my question, where the birth date is indeed the third field

This is the JS I have assigned to it:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").eq(2).val(),"dd/MM/yyyy");
var age = DateTime.dateFrom("19/02/2024","dd/MM/yyyy").diff(birthDate, D'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age',age.years);
var age2 = DateTime.dateFrom("24/07/2024","dd/MM/yyyy").diff(birthDate, D'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age2',age2.years);
});

These are the embedded data fields and the branching I have added:

Luxon is still in the survey header, I haven’t changed anything there:

<script src="https://cdn.jsdelivr.net/npm/luxon@3.4/build/global/luxon.min.js"></script>

Yet, when I enter the birth date as 31/12/2015 and select semester 1 or semester 2, nothing is happening. 


Oops, I typed dateFrom instead of fromFormat. It should be:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").eq(2).val(),"dd/MM/yyyy");
var age = DateTime.fromFormat("19/02/2024","dd/MM/yyyy").diff(birthDate, ['years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age',age.years);
var age2 = DateTime.fromFormat("24/07/2024","dd/MM/yyyy").diff(birthDate, ['years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age2',age2.years);
});

 


Still not branching…

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").eq(2).val(),"dd/MM/yyyy");
var age = DateTime.fromFormat("19/02/2024","dd/MM/yyyy").diff(birthDate, ,'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age',age.years);
var age2 = DateTime.fromFormat("24/07/2024","dd/MM/yyyy").diff(birthDate, ,'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age2',age2.years);
});

If the code is correct, it wouldn’t have something to do with the conditions I set in the branching?


Still not branching…

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").eq(2).val(),"dd/MM/yyyy");
var age = DateTime.fromFormat("19/02/2024","dd/MM/yyyy").diff(birthDate, ,'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age',age.years);
var age2 = DateTime.fromFormat("24/07/2024","dd/MM/yyyy").diff(birthDate, ,'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age2',age2.years);
});

If the code is correct, it wouldn’t have something to do with the conditions I set in the branching?

It could.  I know the code is working. Example


Okay, I don’t know why this happened, but I just completely removed the branching, applied the changes without the branching, then added the branching again and IT IS WORKING!!! Wohoo!

@TomG thank you so very very much, you are amazing!


Leave a Reply