Calculating age based on DOB - Embedded Data now showing up or updating | XM Community
Question

Calculating age based on DOB - Embedded Data now showing up or updating

  • 21 September 2020
  • 8 replies
  • 799 views

Hi all,
I have been follwoing responses by TomG to several questions about calculating age from a given DOB and it seems to work for everyone However, none of mine seem to work. So I am asking for some help. For reference this is the most complete theread I found and a snipet of the code bellow: https://www.qualtrics.com/community/discussion/1397/javascript-to-calculate-users-age-based-on-dob-check-age-eligibility-provide-validation-alert/p2
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
if(age > 18) Qualtrics.SurveyEngine.setEmbeddedData('eligible', '1');
});
My embedded variables like age and eligible do not seem to update regardless of the DOB I give it. I tried just printing the Embedded Data in a text field, and it just shows me empty space. I had the moment.js link in the header, and I use the exact code as above about calculating difference and setting eligible; I included the embedded data before the actual block of where the question is; I even tried using Day.js as an alternative in a following text segment, using the response of the date input:
Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/
        var  given_age = dayjs("${q://QID62/ChoiceTextEntryValue}").format("MM/DD/YYYY");
    
    var curr_date = dayjs().format("MM/DD/YYYY");
    var age = curr_date.diff(given_age, 'year') ;
    Qualtrics.SurveyEngine.setEmbeddedData('age', age);
    

});
But the embedded data never shows and the branching I try to add at the end . It seems I am missing something compeltely obvious, but JS is on the newer languages for me. Any help would be appreciated!🙂


8 replies

Userlevel 7
Badge +27

gre74 ,
Provide a preview link to a survey with the moment example above.

https://www.qualtrics.com/community/discussion/comment/30410#Comment_30410Thanks for finding the time to look it over. I have sent you a message

Badge +1

hi! Were you able to solve your issue? I'm trying to create Age from an Embedded Data Field "Date of Birth" (MM/DD/YYYY format) and am so lost after searching in the community results. I want to create Age simply for reporting purposes in dashboard. Not sure where to create it in survey project or how.

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/37094#Comment_37094https://www.qualtrics.com/community/discussion/comment/37094#Comment_37094https://www.qualtrics.com/community/discussion/comment/30631#Comment_30631Specifying the date format when parsing the birth date solved gre74's issue:
var bdate = moment(jQuery("#"+this.questionId+" .InputText").val(),"MM/DD/YYYY");

Userlevel 6
Badge +5

Hi TomG

Pardon my Javascript ignorance, but I am also trying to get this to work with a text entry question using date content validation and it is not filling in the "age" Embedded Data variable.

I have this JS in the question:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
if(age > 18) Qualtrics.SurveyEngine.setEmbeddedData('eligible', '1');
});

In the header I have this code:


Where would I add the snippet? Do I add it within the Question Javascript?
"ar bdate = moment(jQuery("#"+this.questionId+" .InputText").val(),"MM/DD/YYYY");

Thanks!

Userlevel 6
Badge +5

TomG you can disregard my previous message. The issue was I pasted in the header code from your previous post and qualtrics blocked the email address in there. Thanks for providing this solution though!

I've been successful in implementing TomG 's moment.js date calculation, but I am facing the same issue where I cannot get it to work on a DD/MM/YYY format, only on a MM/DD/YYYY format.
Currently, my code (as provided by TomG ) looks like this:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
  var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');
  Qualtrics.SurveyEngine.setEmbeddedData('age', age);
});

Unfortunately, my very limited understanding of Javascript is preventing me from successfully changing the function to work with the DD/MM/YYY format. How would I implement this solution in my code:
https://www.qualtrics.com/community/discussion/comment/30632#Comment_30632

Userlevel 7
Badge +27

https://www.qualtrics.com/community/discussion/comment/37094#Comment_37094var bdate = moment(jQuery("#"+this.questionId+" .InputText").val(),"DD/MM/YYYY");

Leave a Reply