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 &lt;spanid=xxx&gt;-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.

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

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

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:

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

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:

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":