Calculate Age (in months) from Date of Birth? | XM Community
Solved

Calculate Age (in months) from Date of Birth?

  • 5 October 2021
  • 7 replies
  • 495 views

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!

icon

Best answer by TomG 2 May 2023, 14:09

View original

7 replies

Userlevel 2
Badge +8

Hi, Which question type are you using to ask for birthdate and in which format date will be inserted?

Userlevel 4
Badge +22

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 @TomG refer to use of moment.js for instances like this -- Tom, any of your brilliant review would be greatly appreciated!

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

 

Userlevel 4
Badge +22

Ok, so now I’m trying a different approach (suggested by @TomG in this thread), but still missing the mark because “age” isn’t populating:

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?

Userlevel 5
Badge +19

Hi @JonesE ,

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😊!!

Userlevel 7
Badge +27

Ok, so now I’m trying a different approach (suggested by @TomG in this thread), but still missing the mark because “age” isn’t populating:

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?

@JonesE

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);
});

 

Userlevel 4
Badge +22

Hi @JonesE ,

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, @qualtrics_nerd! Unfortunately, this still didn’t work, even after moving the js and adding page breaks. Tom’s response ended up doing the trick. I appreciate your help!!

Userlevel 4
Badge +22

Ok, so now I’m trying a different approach (suggested by @TomG in this thread), but still missing the mark because “age” isn’t populating:

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?

@JonesE

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, @TomG! This worked perfectly.

@EPx -- please accept Tom’s accept response as the accepted answer for this post!

Leave a Reply