I´m trying to use javascript in a date of birthday question to calculate age of the participant on a specific date (11/09/2001 - dd/mm/yyyy). Because after I´m going to display some questions to people who were older that 18 years at that time. I aldready create “age” in embedded data.
Is this code correct? Because it it not working for me
Qualtrics.SurveyEngine.addOnload(function() {
/* Place your JavaScript here to run when the page loads */
var dobString = "${q://QID212/ChoiceTextEntryValue}";
var dobParts = dobString.split("/");
var dobDate = new Date(dobPartsa2], dobParts21] - 1, dobParts 0]); // Year, Month (0-based), Day
var targetDate = new Date(2001, 8, 11); // 11th September 2001
if (!isNaN(dobDate)) {
var ageInMillis = targetDate.getTime() - dobDate.getTime();
// Calculate age based on milliseconds
var years = ageInMillis / (365.25 * 24 * 60 * 60 * 1000);
// Set the calculated age as embedded data
Qualtrics.SurveyEngine.setEmbeddedData("age", Math.floor(years));
} else {
// Default value in case of invalid dates
Qualtrics.SurveyEngine.setEmbeddedData("ae", -1);
}
});
Qualtrics.SurveyEngine.addOnReady(function() {
/* Place your JavaScript here to run when the page is fully displayed */
// You can add any additional actions you want to perform here
});
Qualtrics.SurveyEngine.addOnUnload(function() {
/* Place your JavaScript here to run when the page is unloaded */
// You can add any cleanup or final actions here
});
Thank you!!!