OpenClinica User Manual/AgeField

Age calculation

edit

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

edit

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.

 
labeling in the CRF


the script

edit

Here is the script and you can put it for example in the instructions column of the section with the items.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script lang="Javascript">
   $.noConflict();
   jQuery(document).ready(function($){
      var dobField = $("#DateOfBirth").parent().parent().find("input");
      var signField = $("#OtherDate").parent().parent().find("input");
      var ageField = $("#CalculatedAge").parent().parent().find("input");
      ageField.attr("readonly", true);
 
      var months = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"];

      function calcYearsBetween(startDate, endDate) {
         var years = (endDate.getFullYear() - startDate.getFullYear());
         if (endDate.getMonth() < startDate.getMonth() || endDate.getMonth() == startDate.getMonth() && endDate.getDate() < startDate.getDate()) years--;
         return years;
      }
 
      function parseDate(input){
         var parts = input.split("-");
         var myDate = new Date();
         myDate.setFullYear(parts[2]);
         myDate.setMonth(months.indexOf(parts[1].toLowerCase()));
         myDate.setDate(parts[0]);
         return myDate;
      }
 
      function yearsBetween(){
         var dob = dobField.val();
         var sign = signField.val();
         var age="";
         var curAge = ageField.val();
         if(dob!=="" && sign!==""){
            age = String(calcYearsBetween(parseDate(dob), parseDate(sign)));
         }
         if(curAge!==age){
            ageField.val(age);
            ageField.change();
         }
      }
      dobField.blur(function(){
         yearsBetween();
      });
      signField.blur(function(){
         yearsBetween();
      });
      $("#srl").on("focus", function () { 
         yearsBetween();
      });
      $("#srh").on("focus", function () { 
         yearsBetween();
      });
});
</script>

How it works

edit

The code begins by including jQuery 1.9.1. You could also point to the one OpenClinica provides.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Furthermore, to prevent a possible conflict with OpenClinica's code, we add the noConflict statement:

$.noConflict();

The script takes the two dates and creates two date objects, based on the day, month and year. 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 uses the number to set the month:

myDate.setMonth(months.indexOf(parts[1].toLowerCase()));

It then calculates the age by subtracting the years and comparing the months and if necessary days:

function calcYearsBetween(startDate, endDate) {
   var years = (endDate.getFullYear() - startDate.getFullYear());
   if (endDate.getMonth() < startDate.getMonth() || endDate.getMonth() == startDate.getMonth() && endDate.getDate() < startDate.getDate()) years--;
   return years;
}

Now we have the age, but we must transfer it to our field, but only if the age has really changed. Furthermore, if the value did change, we call the field's change method, to ensure OpenClinica writes the value to the database:

if(curAge!==age){
   ageField.val(age);
   ageField.change();
}

The age is calculated when we leave the date of birth field, the sign field, or when we press one of the two save buttons, called "srh" and "srl":

dobField.blur(function(){
   yearsBetween();
});
signField.blur(function(){
   yearsBetween();
});
$("#srl").on("focus", function () { 
   yearsBetween();
});
$("#srh").on("focus", function () { 
   yearsBetween();
});