Hi everyone,
I am not very tech-inclined and have little experience with JavaScript (or programming languages, for that matter), so please go easy on me. I have seen similar questions in the community but have found them hard to follow or not applicable (e.g., calculating in years).
I am looking to group my participants in three different ways: A) under 18 months, 18 - 30 months, and C) 30+ months. At the moment, I ask their parent to input their child's age in months and use that number to branch off of, but this can lead to parents miscalculating their child's age in months. I would prefer for the survey to automatically calculate it for them after they enter the child's date of birth.
Any help would be appreciated. Thank you!
Hi, Which question type are you using to ask for birthdate and in which format date will be inserted?
I’d like to resurrect this post since I’m looking for something similar and feel I’m very close to a solution. I’ve seen
For my purposes, I need to calculate age in months based off of the DOB entered in a text field with date validation (mm/dd/yyyy format). Then I’d like to save that value of age (in months) to a variable (‘age’) so that I can use branching logic to determine is a child is within the age parameters of this survey.
- Here is the question in the survey (text entry question with date validation):
- In my survey flow, I’ve set embedded data prior to the block that asks for DOB that creates variables for ‘months’ and ‘age’ (to be completed via JavaScript), as well as ‘currentDate’ which is set to piped text of the current date in m/d/y with leading zero format:
-
Lastly, this is the code I have in the survey on the DOB question:
Qualtrics.SurveyEngine.addOnUnload(function()
{
var dob = moment("${q://QID84/ChoiceTextEntryValue}", "MM/DD/YYYY");
var today = moment("${e://Field/currentDate}", "MM/DD/YYYY");
var age = today.diff(dob, "months")
Qualtrics.SurveyEngine.setEmbeddedData("age", age);
});
Unfortunately, ‘age’ is not populating in my collected data. I’m hoping someone can catch any errors in syntax that I may be overlooking to help make this work?
Thank you!
Erin
Ok, so now I’m trying a different approach (suggested by
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'months');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
});
Any suggestions on what I’m doing wrong?
Hi
You are on the right path.
Just add this JS code on next question after DOB or make a hidden one after DOB.
Since , you are piping the value in the same question and since its value is not set yet.
So, it is giving null value and thus “age ” variable is getting blank data.
${q://QID84/ChoiceTextEntryValue} this value at present is null . Its value will be set after this question.
And, then you can run the JS code.
Hope this resolves your query!!
Ok, so now I’m trying a different approach (suggested by
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'months');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
});
Any suggestions on what I’m doing wrong?
This is the better approach. I’m not sure if it is the only issue, but you didn’t provide a format to parse the date.
Moment has been replaced by Luxon, so it would be better to use that:
Survey header:
<script src="https://cdn.jsdelivr.net/npm/luxon@3/build/global/luxon.min.js"></script>
Age question JS:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").val(),"MM/dd/yyyy");
var age = DateTime.now().diff(birthDate, )'months','days']).toObject();
Qualtrics.SurveyEngine.setEmbeddedData('age',age.months);
});
Hi
You are on the right path.
Just add this JS code on next question after DOB or make a hidden one after DOB.
Since , you are piping the value in the same question and since its value is not set yet.
So, it is giving null value and thus “age ” variable is getting blank data.
${q://QID84/ChoiceTextEntryValue} this value at present is null . Its value will be set after this question.
And, then you can run the JS code.
Hope this resolves your query!!
Thank you,
Ok, so now I’m trying a different approach (suggested by
Qualtrics.SurveyEngine.addOnPageSubmit(function()
{
var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'months');
Qualtrics.SurveyEngine.setEmbeddedData('age', age);
});
Any suggestions on what I’m doing wrong?
This is the better approach. I’m not sure if it is the only issue, but you didn’t provide a format to parse the date.
Moment has been replaced by Luxon, so it would be better to use that:
Survey header:
<script src="https://cdn.jsdelivr.net/npm/luxon@3/build/global/luxon.min.js"></script>
Age question JS:
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").val(),"MM/dd/yyyy");
var age = DateTime.now().diff(birthDate, )'months','days']).toObject();
Qualtrics.SurveyEngine.setEmbeddedData('age',age.months);
});
THIS IS IT. Thank you,
Leave a Reply
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.