OpenClinica User Manual/AgeField
Age calculation
Sometimes you may want to calculate the age of a StudySubject, for example at the date on which the Informed Consent is signed. You can do this by using the following script, which uses three CRF-items:
- date of birth, DateOfBirth,
- an other date, OtherDate, such as the date of signing the informed consent and
- the field that will hold the calculated age, CalculatedAge.
Identifying the three items
The jscript uses three variables it reads from the CRF-items, but before it can do that, we must label them in the CRF. We do this by using the <spanid=xxx>-tag. We put these labels in either the left- or the right-item-text.
the script
Here is the script and you can put it for example in the instructions column of the section with the items.
<script> var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; jQuery(document).ready(function($) { function calculateAge(){ // retrieve values from the two date fields var value1 = fieldDateOfBirth.val(); var value2 = fieldOtherDate.val(); var currAge = fieldAge.val(); // check whether both fields are filled if(value1!="" && value2!=""){ // split and deterimine day month and year of birthday var splitString = value1.split("-"); var day1 = splitString[0]; var year1 = splitString[2]; var mon1 = months.indexOf(splitString[1].toLowerCase())+1; // split and deterimine day month and year of other field splitString = value2.split("-"); var day2 = splitString[0]; var year2 = splitString[2]; var mon2 = months.indexOf(splitString[1].toLowerCase())+1; // calculate age var age=year2-year1; if((mon2==mon1 && day2<day1)||mon2<mon1) age--; if(currAge != age){ // set outcome field to age fieldAge.val(age); fieldAge.change(); } } } // fire when save is pressed $("#srl").focus(function(){ calculateAge(); fieldAge.attr('disabled', ''); }); $("#srl").blur(function(){ fieldAge.attr('disabled', 'disabled'); }); $("#srh").focus(function(){ fieldAge.attr('disabled', ''); calculateAge(); }); $("#srh").blur(function(){ fieldAge.attr('disabled', 'disabled'); }); var fieldDateOfBirth = $("#DateOfBirth").parent().parent().find("input"); var fieldOtherDate = $("#OtherDate").parent().parent().find("input"); var fieldAge = $("#CalculatedAge").parent().parent().find("input"); fieldAge.attr('disabled', 'disabled'); }); </script>
How it works
The script takes the two dates and splits them into three parts: day, month and year. These are stored in six variables: day1 and 2, month1 and 2 and year1 and 2. Then the months are compared with the array of available months, which is declared in the start of the script. As you can see these are given in the English notation, so change these abbreviations if your users have another browser language. The script converts the given name to lowercase and then finds the ordinal number of the month and adds 1 in:
var mon1 = months.indexOf(splitString[1].toLowerCase())+1;
It then calculates the age by subtracting the years and compensating for the months and if needed the days.with
var age=year2-year1; if((mon2==mon1 && day2<day1)||mon2<mon1) age--;
Now we have the age, but we must transfer it to our field, but only if the age has really changed.
if(currAge != age){ // set outcome field to age fieldAge.val(age); fieldAge.change(); }
Finally we must set the attribute change for this field, otherwise OpenClinica won't write the data to the database, so that's why we added the last line:
fieldAge.change();
We do all this when one of the two Save-buttons has the focus and these are called "srh" and "srl". But before we can do this, we must set the attribute "disabled" to not-disabled, because otherwise we can not set the value of the input.
