calculation of an age from an embedded data | XM Community
Skip to main content

I know that the question is asked very often but I can not understand the answers already posted.
At the start of a survey, I get a date of birth in the format DD/MM/YYYY (embedded data) and I want to perform a calculation in the background (survey flow) "Current day - Date of birth". but the date of birth is not in the correct format for this.
how to modify this format? or how to do otherwise via a hidden question?

I found my answer here : https://www.qualtrics.com/community/discussion/12534/how-create-new-survey-embedded-data-age-from-embedded-birthdate-for-reporting-purposes
to sum up :
Define the embedded data field 'age' at the beginning of the survey flow.
Add moment.js to your survey header : ">https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js">
Add the following JS to a question:
Qualtrics.SurveyEngine.addOnload(function() {
var bdate = moment("${e://Field/BirthDate}","DD/MM/YYYY");
Qualtrics.SurveyEngine.setEmbeddedData('age',moment().diff(bdate,'years'));
});


Leave a Reply