Calculate Age From Birthday

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:

Setup

  1. First, add your date of birth Textbox question.  
  2. 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
  3. Next, add a field to store your calculated age. This can be a Textbox or a Hidden Value. 
  4. 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
  5. 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!

Basic Standard Market Research HR Professional Full Access Reporting
Free Individual Team & Enterprise
Feature Included In