This script allows you to ask for participants' birth months in a Single Choice Drop Down question, and birth years in a Number question. Their responses can then be saved into hidden Date of Birth and Age questions.
Note:
- The Age value is static and captured at the time the script executes, and won't change over time.
- The Date of Birth value is calculated based on the first day of the given month and the given year.
Detailed Steps
- In your survey, add a Page element.
- Add a Single Choice question to the Page element.
- Set Display answers as to Dropdown.
- Add all the months as answers (January to December).
- Under the Single Choice question, add a Number question.
- Set Display answers as to Slider.
- Set Minimum and maximum values to the lowest and highest birth years, respectively (for example, 1920 and 2020).
- Add a second Page element.
- Add a Date question to the second Page element.
This question will capture the Date of Birth value. You may want to temporarily leave this unhidden for testing. Set the minimum and maximum values for the years (same as the values in step 3). - Add a Number question under the Date question.
This question will capture the Age value. You may want to temporarily leave this unhidden for testing. - Open the first Page element for editing and click the Scripting button.
- Copy and paste the script below into the On Complete tab.
- Replace Age_Month with the name of the Single Choice question that asks for the birth month.
- Replace Age_Year with the name of the Number question that asks for the birth year.
- Replace DOB with the name of the Date question that captures the Date of Birth value.
- Replace Age with the name of the Number question that captures the Age value.
- Click Save.
- Preview and test your survey.
Script
/* Script Name: How do I capture participants' birthdates and ages based on month
and year responses? (Modern Surveys) To ensure your script works as expected, always copy the original script from the Script
Library found in the Alida Help Center. Failure to use the latest script from the script library
may cause unexpected results. */ const month = response.getDataPoint("Age_Month");
const year = response.getDataPoint("Age_Year");
const age = response.getDataPoint("Age");
const combined = response.getDataPoint("DOB");
// Get the entered data
var m = month.getSelectedIndex();
var y = year.get();
// New date object on the 1st of the given year and month
var date = new Date(y, m, 1);
// Calculate age
var diff_ms = Date.now() - date.getTime();
var age_dt = new Date(diff_ms);
var age_r = age_dt.getFullYear() - 1970;
//Add validation in case the date is in the future
if(age_r < 0)response.setError("Age_Month","Please do not enter a date in the future.");
//Save result into date question and numeric
age.set(age_r);
combined.set(date);
Example
Sample survey structure:
First page asking participants for birth month and birth year:
Second page capturing date of birth and age:
Comments
0 comments
Article is closed for comments.