This JavaScript will calculate age from the date of birth entered in an open-text field.
Check it out in an example survey.
OR
Add a survey with this setup to your account!
This script uses the following question types or actions:
- Textbox Question (for Birth Date)
- Textbox Question (for Age) or Hidden Value
- Javascript Action
Setup
- First, add your date of birth Textbox question.
- Go to the Layout tab of your date of birth question and add the following to the CSS Class Name field:
- Enter dob if you are using the DD/MM/YYYY format
- Enter dob2 if you are using the MM/DD/YYYY format
- Next, add a field to store your calculated age. This can be a Textbox or a Hidden Value.
- On the Layout tab of your field that stores age add the following to the CSS Class Name field:
- Enter age if you are using the DD/MM/YYYY format
- Enter age2 if you are using the MM/DD/YYYY format
- Next, add a Javascript action to the same page and add one of the below JavaScript code snippets.
If your DOB question is in MM/DD/YYYY format copy and paste this Javascript:
//calculate age from MM/DD/YYYY formatted date question
//add CSS class "dob2" to date question, "age2" to textbox to store the age
$(document).ready(function() {
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if (isMobile.any()) {
// It is mobile
function calculateAge(birthday) {
var ageDifMs = Date.now() - birthday.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
$(".dob2 :input").change(function() {
var birthday = $(this).val();
birthday = birthday.split("-");
birthday = new Date(birthday[0], birthday[1] - 1), birthday[2];
var age = calculateAge(birthday);
$(".age2 :text").val(age);
});
} else {
function calculateAge(birthday) {
var ageDifMs = Date.now() - birthday.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
$(".dob2 :text").change(function() {
var birthday = $(this).val();
birthday = new Date(birthday);
var age = calculateAge(birthday);
$(".age2 :text").val(age);
});
}
});
If your DOB question is in DD/MM/YYYY format copy and paste this Javascript:
//calculate age from DD/MM/YYYY formatted date question
//add CSS class "dob" to date question, "age" to textbox to store the age
$(document).ready(function() {
var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if (isMobile.any()) {
// It is mobile
function calculateAge(birthday) {
var ageDifMs = Date.now() - birthday.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
$(".dob :input").change(function() {
var birthday = $(this).val();
birthday = birthday.split("-");
birthday = new Date(birthday[0], birthday[1] - 1), birthday[2];
var age = calculateAge(birthday);
$(".age :text").val(age);
});
}
else {
function calculateAge(birthday) {
var ageDifMs = Date.now() - birthday.getTime();
var ageDate = new Date(ageDifMs);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}
$(".dob :text").change(function(){
var birthday = $(this).val();
birthday = birthday.split("/");
birthday = new Date(birthday[2], birthday[1] - 1), birthday[0];
var age = calculateAge(birthday);
$(".age :text").val(age);
});
}
});
Limitation
Please note that if your survey respondents enter the date in the wrong format, for example MM-DD-YYYY instead of MM/DD/YYYY, the date will be stored in the requested format but the above script(s) will not be able to evaluate the incorrectly entered date.
Scripting and Other Custom Solutions
We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.
If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your needs. Beyond this, check out our Professional Services; these folks have the scripting chops to help you to achieve what you are looking for!
Admin
— Dave Domagalski on 03/21/2018
@Dave: Thank you so much for the kind words!
I'm happy to hear that the example helped address your needs!
Thank you again!
David
Documentation Specialist
SurveyGizmo Customer Experience
— Ying on 03/20/2018
This is really helpful. I got asked for it to be included in a survey and the example did just what I need it to do
Admin
— Bri Hillmer on 08/03/2017
@Alanna: Modifying the script like you describe is beyond my scripting abilities. Our check our Programming Services team have the scripting chops to help you to achieve what you are looking for! You can learn more here:
https://www.surveygizmo.com/programming-services/
I hope this helps!
Bri Hillmer
Documentation Coordinator
SurveyGizmo Customer Experience Team
— Alanna on 08/02/2017
HI SG team. This is a great feature we which currently have incorporated into our survey. As I'm not familiar with Javascript, do you have any advice regarding the calculating age in terms of Months and Years. The above coding is limited to whole numbers (e.g.,20 yrs), and we're interested in also capturing months (e.g., 3 mths), so we can redirect respondents to the appropriate page based on the age of their child.
Admin
— Bri Hillmer on 11/02/2015
@Byron: I took a quick look at your survey and it looks like you are using this script for something it wasn't built to handle. This script was designed to take a birth date and today's date and figure an age. As you have it implemented later in the survey calculate age at current house this script will not accomplish this. I would recommend computing that data after the data has been collected.
I hope this helps!
Bri Hillmer
Documentation Coordinator aka Survey Sorceress
SurveyGizmo Support
— Byron on 11/01/2015
Hello,
I used this code to calculate the age from a DOB input, it works great. When I try to use the same code again, the code calculates the value, based on the logs, but it never loads it into the age text box. I used the same code, just updated the element fields. Any ideas? Is there a way to see the all system logs.. where the code is failing... it really should fail.. since I used the same code. Please, urgent help needed.