Calcuting Age from Calendar Question entry | Experience Community
Skip to main content
Question

Calcuting Age from Calendar Question entry

  • March 5, 2026
  • 1 reply
  • 23 views

Forum|alt.badge.img

Hi,

In my survey, I have the calendar-type questions so participants can enter their date of birth. I would like to be able to calculate age from this entry. I need the participants' ages to apply branch logic, as different age groups require different sets of questions. 

I have tried multiple times using Javascript in the calendar question, adding embedded date in my survey flow and them adding branch logic based off the embedded (‘age’) but nothing seems to work. 

Any suggestions?

Thanks for your help! 

1 reply

nikamshubham73
Level 1 ●
Forum|alt.badge.img+1

Hi ​@bhilab,

As you are using Calendar type question, assuming the new survey taking experience has been enabled in your survey. The JS code might run but there is a catch while setting up the syntax.

Step 1: Place the below JS code in your Calendar question

Qualtrics.SurveyEngine.addOnPageSubmit(function () {

var dobValue = document.querySelector(".text-input").value;

if (!dobValue) return;

var dateOnly = dobValue.split(" ")[0];

var dob = new Date(dateOnly);

var today = new Date();

var age = today.getFullYear() - dob.getFullYear();

var m = today.getMonth() - dob.getMonth();

 if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {

 age--;

 }

console.log("Age calculated:", age);

Qualtrics.SurveyEngine.setJSEmbeddedData("Age", age);

});

Step 2: Simply declaring the variable as Age in the Survey Flow will not automatically store the value calculated through JavaScript. You need to declare it in the same nomenclature as “__js_Age”. This will contain the exact years of Age from DOB. The pipe text would look like “${e://Field/__js_Age}” 

Step 3: Since you have multiple Calendar questions, repeat the above steps and you can use the (__js_Age_1, __js_Age_2...) embedded variables to set the branch logic.

Reference from: