Calculating age from DOB (DD/MM/YYYY) using embedded data (NOT JavaScript)
Does anyone know how to calculate age from DOB (DD/MM/YYYY) using embedded data in the Survey flow section and NOT using JavaScript.
I’ve looked through previous threads but they all seem to use JavaScript and a different date format.
At the moment, I’m playing with the below but I’m not sure it’s correct:
Round((${date://CurrentDate/d%2Fm%2FY}-${q://QID”insert DOB question ref”/ChoiceTextEntryValue})/365,2)
Page 1 / 1
@sally_
Use this math operation $e{ ${date://CurrentDate/Y} - q://QIDYearofBirth/ChoiceTextEntryValue }
But make sure in the question where they type in year of birth, you have to add Number Validation. And there’s page break between that question and where you show the age.
If you want to use DD/MM/YYYY format, then you have to use code to breakout value because all that slash making it not a number to be calculated by math operator
Thanks so much Nam! How do you breakout the DD/MM/YYYY value? Is that included in the equation you shared in your response?
Thanks so much Nam! How do you breakout the DD/MM/YYYY value? Is that included in the equation you shared in your response?
@sally_ No my formula simply subtract this year from the respondent year of bird (you have to create that question and replace it in the formula with pipe-text).
If you want to use DD/MM/YYYY format, then you have to use code to breakout value because all that slash making it not a number to be calculated by math operator
Like I said, you have to use code
Thanks Nam! I need their exact age though, not just current year minus year of birth.
Do you know how to calculate exact age i.e., current DD/MM/YYYY minus birth DD/MM/YYYY?
When I tried the formula above, I got this result: 2023.99673858504766683392
I have a validation setting applied to the question and I’m trying to generate age as an embedded value, not to display back to the participate.
Thanks Nam! I need their exact age though, not just current year minus year of birth.
Do you know how to calculate exact age i.e., current DD/MM/YYYY minus birth DD/MM/YYYY?
When I tried the formula above, I got this result: 2023.99673858504766683392
I have a validation setting applied to the question and I’m trying to generate age as an embedded value, not to display back to the participate.
@sally_ You said clearly NOT to use JavaScript, so calculating both month and date into the age gonna use JavaScript. You okay with that?
Hi @Nam Nguyen, if JavaScipt is the only way to do this, then I am happy to use it I was hoping to avoid it, but this doesn’t seem possible.
@sally_ Insert this to your birthday text entry question. The age will be saved in embedded data named ‘Age’. This code will not work with simple layout
Qualtrics.SurveyEngine.addOnReady(function() { let dateInput = this.getQuestionContainer().querySelector("input"); dateInput.type = "date";
dateInput.addEventListener("change", function() { let dob = new Date(dateInput.value); let today = new Date(); let age = today.getFullYear() - dob.getFullYear(); let monthDiff = today.getMonth() - dob.getMonth();
Test it with a date before and after to day, you will see the difference
Thanks @Nam Nguyen. However, I am using the simple layout due to accessibility considerations for my participants. Is there a way to do this using the simple layout?
Thanks @Nam Nguyen. However, I am using the simple layout due to accessibility considerations for my participants. Is there a way to do this using the simple layout?
Then change the code to setJSEmbeddedData but now you have to pipe-text the result in different way as: ${e://Field/__js_Age}
Thanks @Nam Nguyen!
Sorry, I’m having a bit of trouble with the code. The updated version (based on your instructions for how to adapt this for the simple layout) is below. However, when I add this to the “Edit Question JavaScript” section, I keep getting the following error when I click “Save”. I’ve checked and there are equal numbers of the curly brackets. Have I done something wrong?
Error: Invalid JavaScript! You cannot save until you fix all errors: Unexpected token {
Code:
Qualtrics.SurveyEngine.addOnReady(function() { let dateInput = this.getQuestionContainer().querySelector("input"); dateInput.type = "date";
dateInput.addEventListener("change", function() { let dob = new Date(${e://Field/__js_Age}); let today = new Date(); let age = today.getFullYear() - dob.getFullYear(); let monthDiff = today.getMonth() - dob.getMonth();
If you want an always accurate age you should use Luxon. See this code. Change the date format from MM/dd/yyyy to dd/MM/yyyy.
@sally_ Nah, not that, just change the last line
Qualtrics.SurveyEngine.addOnReady(function() { let dateInput = this.getQuestionContainer().querySelector("input"); dateInput.type = "date";
dateInput.addEventListener("change", function() { let dob = new Date(dateInput.value); let today = new Date(); let age = today.getFullYear() - dob.getFullYear(); let monthDiff = today.getMonth() - dob.getMonth();
When I talked about pipe-text the result in different way as: ${e://Field/__js_AgeRespondent} I mean if you want to show or save the result, you have to state it as __js_AgeRespondent because you’re using Simple Layout
Thanks @TomG. When you say “always accurate” - does this mean the age stored in the embedded data keeps updating? I need it to stay static, so I know age at time of survey completion.
Thanks @Nam Nguyen! So sorry to keep bothering you, but when I test the survey I receive an error for that question now, that didn’t used to appear before?
Thanks @Nam Nguyen! So sorry to keep bothering you, but when I test the survey I receive an error for that question now, that didn’t used to appear before?
@sally_ Turn off the validation, the JS code already force them to input a date
Thanks @Nam Nguyen! This is great Sorry I have one last question, I notice that the embedded field doesn’t have any decimal places. Does this mean the age figure is rounded to the nearest whole number? If so, what are the rules governing this? I need to send participants who are 18.0=< to one branch of the survey, and participants who are <18.0 to a different branch.
Thanks @Nam Nguyen! This is great Sorry I have one last question, I notice that the embedded field doesn’t have any decimal places. Does this mean the age figure is rounded to the nearest whole number? If so, what are the rules governing this? I need to send participants who are 18.0=< to one branch of the survey, and participants who are <18.0 to a different branch.
@sally_ Not rounded. It’s base on your birth day. Test it with a date before and after to day, you will see the difference. 04/11 and 06/11 will give you different answer.
Thanks @Nam Nguyen! I really appreciate your help :)
Thanks @TomG. When you say “always accurate” - does this mean the age stored in the embedded data keeps updating? I need it to stay static, so I know age at time of survey completion.
No, I mean at time of calculation the age will always be accurate.