/
5.21.4.1.1 Calculating Age From Date of Birth

5.21.4.1.1 Calculating Age From Date of Birth

This example will show you how to use a JavaScript field to calculate a clients age from their date of birth.

Script

 

Calculating Age
// The first field specified is the one on which the Javascript field shall depend on.
var inputDate = $("#CustomField_6040").val();
    var today = new Date();
// This line controls the formatting of the data output in the JavaScript Field
    var birthDate = new Date(inputDate.split('/')[2],inputDate.split('/')[1]-1,inputDate.split('/')[0]);
// The script below pull out the clients date of birth and the current date in order to calculate their age.
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) 
    {
        age--;
    }
// This is the ID of the new JavaScript field
 $("#CustomField_6039").html(age);

 

Age calculated from Date of Birth

 

This screenshot shows an age calculated from the date of birth above.

 

New age calculated from changed Date of Birth

 

 

We can see the JavaScript field in action if the date of birth is changed. The age will be automatically updated to reflect the new date of birth

Related content