# Calculate the age using a year or year and month variable: EDC/CDMS

As a full birth date might be considered personally identifiable information, using only the birth year or year and month of birth is a good alternative. It is still possible to calculate the age from these variables using a calculation field (although with less precision).

### Calculate the age of a patient from a birth year

``````var a = moment();
var b = moment('{dem_pat_birthyear}', 'YYYY');
var diff = a.diff(b, 'years'); // calculates patient's age in years
diff; // this prints out the age``````

In this calculation {dem_pat_birthyear} will need to be replaced with your variable name for birth year.

Note: In the first line, moment(); is used to automatically calculate today's date. This date will be updated once the data entry for a record is opened.

### Calculate the age from a birth year and a month using a text field

If you are using a text field to collect only month and year of birth, you can use the following template to calculate the age:

``````var a = moment();
var b = moment('{dem_pat_birthyear}', 'MM-YYYY');
var age = moment.duration(a.diff(b));
var years = age.years();
var months = age.months();
"The age is " + years + " years " + months + " months ";``````

In this calculation {dem_pat_birthyear} will need to be replaced with your variable name for birth year.

### Calculate the age of a patient from the birth year and a visit date

``````var birthyear = moment('{patient_birthyear}', 'YYYY');
var visitdate = moment('{baseline_visit}', 'DD-MM-YYYY');
visitdate.diff(birthyear, 'y');``````

In this calculation {patient_birthyear} will need to be replaced with your variable name for birth year and {baseline_visit} for the visit date. Test this calculation in the calculation helper.

To calculate the age using an actual date field, use this template.

### Calculate the age of a patient from a birth year and display it in years in months

When you want to show age in years and months, for example for children, you can use the following formula.

``````var date1 = "{dob}".split("-");
var jsdate1 = new Date(date1 [2], date1 [1]-1, date1 [0], 0, 0, 0);
var date2 = "{visit_date}".split("-");
var jsdate2 = new Date(date2 [2], date2 [1]-1, date2[0], 0, 0, 0);
var diff = Math.floor(jsdate2.getTime() - jsdate1.getTime());
var day = 1000 * 60 * 60 * 24;
var days = Math.floor(diff/day);
var months = Math.floor(days/31);
var years = Math.floor(months/12);
months = months - (12*years)
years + " years, "+ months + " months";``````

You can test this template in the calculation helper.

### Calculate the age of a patient from a birth year and birth month and display age in years

``````var string = '{birthmonth}' + '-' + '{birthyear}';
var dob = moment(string, 'MM-YYYY');
var date = moment('{date_compl}', 'DD-MM-YYYY');
var diff = date.diff(dob, 'y');
diff;``````

In this calculation {birthmonth} and {birthyear} will need to be replaced with your variables name for birth year and birth month. You can also test this calculation in the calculation helper tool.