Can I auto-populate age Q4, from the question Date of Birth Q3? | XM Community
Skip to main content

Screenshot 2022-12-30 at 15.47.12.png // Get the birthdate input
  var birthdateInput = this.getQuestionInput('Q4'); // Replace 'Q4' with the actual Question ID of your birthdate question
   
  // Calculate the age
  var today = new Date();
  var birthdate = new Date(birthdateInput);
  var age = today.getFullYear() - birthdate.getFullYear();
  var m = today.getMonth() - birthdate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthdate.getDate())) {
    age--;
  }
   
  // Output the age
  console.log(age); // You can replace 'console.log' with any other method of outputting the age, such as displaying it on the page or storing it in a hidden question
});

First_Inspire

First, I would change the birthdate question to a calendar picker using the JavaScript found from Flatpickr.


Place in the Header:


https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css" rel="stylesheet" />

Add JavaScript to the question: 


Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
jQuery("#"+this.questionId+" .InputText").flatpickr({
dateFormat: "d/m/Y"
});

I would then use moment.js to calculate the age.


Create Embedded data field:


Create "age" and set value to "x"

Place in the Header:


">https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js">

Add JavaScript to the question:


Qualtrics.SurveyEngine.addOnload(function() {
  var bdate = moment("Add Birthday Question ID","MM/DD/YYYY");
  Qualtrics.SurveyEngine.setEmbeddedData('age',moment().diff(bdate,'years'));
});

Last, Pipe Text the embedded data field "Age" into the age question.


Be sure there is a page break between Q3 & Q4




@jmborzick this might be a stupid question, but how does one add multiple things to a header? My survey works fine up until creating a calendar picker for dob but when it comes to calculating age after, it does not seem to work. Thank you for your help!


@JA0208 Not a stupid question at all. One problem is that the code I pasted in the previous response was incorrect.

For the age calculation you’ll need to have the following in the header: 

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>

For the date picker, you’ll need to have the following header: 

<link href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css" rel="stylesheet" /><script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>

 

To answer your question directly, your header should contain the following: 

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>

<link href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css" rel="stylesheet" /><script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>


@jmborzick  Thank you for your response! No matter what I do, I cannot seem to set the value for the “age” embedded variable. Do you have any ideas why that could be? I have been stuck for 10 days and losing all hope.


@JA0208 Are you able to share the QSF with me? 


@jmborzick unfortunately it’s from my work account and I do not have the liberty to do that - but happy to provide any specifics you need! Sorry it’s so complicated


@JA0208 Can you send me a screenshot of your survey flow? In the survey flow, indicate the questions where the date picker is as well as the question where the JavaScript is for the age calculation. 


@jmborzick I have a first block (Eligibility) which has both the DOB and age questions. Then I have an embedded data field “Age” which is set as x. I have kept the JS codes exactly as on this chat


@JA0208 

In the survey flow, place “Age” above the blocks with JavaScript. 

 

Make sure there is a page break between the two Questions with JavaScript

 

Make sure that the capitalization for age is the same in both the JavaScript and survey flow. 


@jmborzick oh my god, game changer! I will try this out immediately


@jmborzick just fixed!!!! thank you so much I have been racking my brain on this for so long


Leave a Reply