Calculating age from participant-entered DOB | XM Community
Skip to main content
Question

Calculating age from participant-entered DOB


I have looked through many previous answers and have come to the conclusion that I know too little about Javascript so please be patient.

I have found a suggestion to use luxon so have added this to a footer in Look and Feel

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

 

I have set the following embedded fields at the start:

Age

DateTime

And then after the parent has entered their child’s date of birth (it’s a field with a content check, and has to be DD/MM/YYYY) I have set

BirthDate = ${q://QID1215690757/ChoiceTextEntryValue}

 

I have looked at this code 

 

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").val(),"MM/dd/yyyy");
var age = DateTime.now().diff(birthDate, r'months','days']).toObject();
Qualtrics.SurveyEngine.setEmbeddedData('age',age.months);
});

which is from this question

https://community.qualtrics.com/custom-code-12/calculate-age-in-months-from-date-of-birth-16970?sort=dateline.desc

However, I am not sure what I need to do next.

As a test, I tried using just the line 

var DateTime = luxon.DateTime;

followed by 
Qualtrics.SurveyEngine.setEmbeddedData('DateTime',DateTime);

However, it did not appear when I piped the embedded data into a later question.

 

I also tried just
 

Qualtrics.SurveyEngine.addOnload(function()
{
Qualtrics.SurveyEngine.setEmbeddedData( 'DateTime', 18 );
});

 

But again I just got a blank when I piped this into another question.
 

Any suggestions? 
 

thanks very much

12 replies

Badge +2

Hi @TomG - I wonder if you can help with this.

thanks!

Userlevel 7
Badge +39

Hey @KatieA ,

2 things you need to change you have made validation of DD/MM/YYYY but the code checks MM/DD/YYYY so change that and your age here is getting calculated in months, you can use below code to get in years. You should see it on next screen the Age embedded data getting filled.

You can update it to:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").val(),"dd/mm/yyyy");
var age = DateTime.now().diff(birthDate, ['years','months','days']).toObject();

Qualtrics.SurveyEngine.setEmbeddedData('Age',age.years);
});

 

Badge +2

Hi, I will try that, but do you have any idea why I can’t even get the minimal code I tried to work?

I have set BirthDate as embedded data after the parent enters the birth date.
Is it better to use the above code, and therefore, my 

 

QID1215690757

 

replaces 

questionId

Or is the replacement more complex than that?

thanks!

Edit: I tried the code with the replacement I suggested and it does not work - so I’m guessing I have the idea wrong about the edits to the code based on my question numbering.

When I create the embedded data, that’s the code for the question.
However in the survey it is Q5.

Badge +2

Hmm I tried to reply but the message has disappeared.

I tried that code and with substituting 

questionID

with 

QID1215690757

this still showed a blank.

Userlevel 7
Badge +39

@KatieA 

Are you using simple layout?

 

Badge +2

I was. I just switched to Flat. I also noticed that my embedded data field was AGE and the code above has Age so I changed that. Still blank!

 

As I said previously, when I tried to make an embedded data field that was just DateTime it didn’t show that either. Nor when I tried to make an embedded data field that was constant. I have a feeling it’s something to do with either creating embedded data fields in JS or showing embedded data fields that have been created in JS. I can show embedded data fields that are either created from the URL or defined in an opening block just fine.

Userlevel 7
Badge +27

@KatieA,

The code at this links works with any layout:

https://gist.github.com/marketinview/543413f36b34ba8bc71d97d4fd489b9d

Badge +2

Thanks, I will try that.

Is it sufficient to create embedded data birthDate which pulls from the parent-entered DOB, and then the JS appears after that variable is created, or do I need to refer to that parent-entered question in the code somehow?

(This still doesn’t give anything except a blank in the output, as it is, which is why I’m asking what else I need to do!)

Userlevel 7
Badge +27

@KatieA,

Using the code in the the link I provided, add the JS to the birthdate text entry question. To save age in your response data, add the embedded data field (__js_age) to the survey flow prior to the block that contains the birthdate question.

Badge +2

OK, so embedded data at the start of the survey flow:

__js_age

and

DateTime

This works when the question asking for DOB is in the individual survey - but originally I had it in a library block. I suspect this may be the issue, either because you just can’t do this, or because I can’t add a header to the library block.
It is a lot more convenient to have exactly the same library block for all my parent surveys asking the boilerplate questions (child’s first name, date of birth, school or nursery, etc.) but I guess I’ll have to do without that!

Thanks so much
Katie

Badge +2

I spoke too soon.

It stopped working, and I created a survey with literally two questions and two embedded variables:

Embedded data:

__js_age

DateTime

Question 1
What is your child’s DOB (dd/mm/yyyy format) - (with content validation).

This question has the javascript.

New block

Question 2

We have calculated your child's age as ${e://Field/__js_age}.

Results of question 2:

We have calculated your child's age as .

Badge +2

Sorry folks but this still isn’t solved.

Looking at the Console for the DOB question I get the following:

 

 

vendor.944ec92….js:1 Failed to evaluate page script in page Uncaught TypeError: Cannot read properties of undefined (reading 'getTop')https://lancasteruni.eu.qualtrics.com/jfe/static/dist/jfe.1b3ffa7….js:1:113107



and

Uncaught TypeError: Cannot read properties of undefined (reading 'getTop')
    at S.r.scrollToQuestion (jfe.1b3ffa711d393bc6ebee.js:1:93407)
    at S.r.i [as scrollToQuestion] (vendor.944ec92fd0786e9660e8.js:1:210335)
    at S.r.scrollToQuestion (preview.0c32e05edbd7b2e5b509.js:1:2776)
    at S.r.i [as scrollToQuestion] (vendor.944ec92fd0786e9660e8.js:1:210335)
    at jfePreview.1c0f53118e2975fa6a18.js:1:304947
    at Array.map (<anonymous>)
    at Object.autoScroll (jfePreview.1c0f53118e2975fa6a18.js:1:304929)
    at S.r.autoScroll (preview.0c32e05edbd7b2e5b509.js:1:2952)
    at S.r.i [as autoScroll] (vendor.944ec92fd0786e9660e8.js:1:210335)
    at S.r._pageScrolled (preview.0c32e05edbd7b2e5b509.js:1:2594)

 

Does this give any clues at all?

Leave a Reply