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

Can I auto-populate age Q4, from the question Date of Birth Q3?

  • December 30, 2022
  • 11 replies
  • 217 views

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
});

11 replies

jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • QPN Level 5 ●●●●●
  • 261 replies
  • January 3, 2023

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




Forum|alt.badge.img
  • 6 replies
  • January 26, 2024

@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!


jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • QPN Level 5 ●●●●●
  • 261 replies
  • January 26, 2024

@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>


Forum|alt.badge.img
  • 6 replies
  • January 26, 2024

@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.


jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • QPN Level 5 ●●●●●
  • 261 replies
  • January 26, 2024

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


Forum|alt.badge.img
  • 6 replies
  • January 26, 2024

@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


jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • QPN Level 5 ●●●●●
  • 261 replies
  • January 26, 2024

@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. 


Forum|alt.badge.img
  • 6 replies
  • January 26, 2024

@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


jmborzick
QPN Level 5 ●●●●●
Forum|alt.badge.img+41
  • QPN Level 5 ●●●●●
  • 261 replies
  • January 26, 2024

@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. 


Forum|alt.badge.img
  • 6 replies
  • January 26, 2024

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


Forum|alt.badge.img
  • 6 replies
  • January 26, 2024

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