Start a conversation

Check if the last visit date matches the end of study date

The calculation below allows to check all previous visit dates and compare the date of the last visit with the date of the end of the study. If the last visit date matches the end of study date, the calculation will output 'Yes', otherwise 'No'.

'##allowempty##';
'##setemptytozero##';

var screen = moment('{scr_date}', 'DD-MM-YYYY');
var visit_1 = '{v1_date}';
var visit_2 = '{v2_date}';
var visit_3 = '{v3_date}';
var visit_4 = '{v4_date}';
var visit_5 = '{v5_date}';
var end_study = moment('{eos_date}', 'DD-MM-YYYY');

var visit_dates = [screen, visit_1, visit_2, visit_3, visit_4, visit_5];

var i;
var last_date;

for (i = 0; i < visit_dates.length; i++) {
    if (visit_dates[i] != 0) {
        last_date = visit_dates[i];
    }
}

var last_visit_date = moment(last_date, 'DD-MM-YYYY');

if (end_study.isSame(last_visit_date)){
    "Yes.";
} else {
    "No.";
}

Check the calculation in the calculation field helper.

Step by step

1. First, we will use the '##allowempty##' and '##setemptytozero##' tags to make sure that the dates which have not been completed are set to 0:

'##allowempty##';
'##setemptytozero##';

2. Dates in Castor EDC are stored as strings. To make sure we can perform calculations with the dates,, we need to convert them to the date format. Here we are retrieving the screening ('{scr_date}') and the end of study dates ('{eos_date}') and converting them to the right format using the moment.js library. For the other dates, we simply assign them to the variables which can be used within our calculations. We do not perform the conversion yet, as we only need to check the date of the last performed visit.

var screen = moment('{scr_date}', 'DD-MM-YYYY');
var visit_1 = '{v1_date}';
var visit_2 = '{v2_date}';
var visit_3 = '{v3_date}';
var visit_4 = '{v4_date}';
var visit_5 = '{v5_date}';
var end_study = moment('{eos_date}', 'DD-MM-YYYY');

3. We create an array ('list') visit_dates and include the screening date and all the visit dates. Then we initialize a counter i and last_date variables. Using the counter variable in the for loop, allows us to find the date of the last visit from our array. This is done by checking each individual date. If a date is not equal to zero which is checked in the expression if (visit_dates[i] != 0), it means that the value was entered in Castor EDC. The date of the last visit is then saved in the last_date variable.

var visit_dates = [screen, visit_1, visit_2, visit_3, visit_4, visit_5];

var i;
var last_date;

for (i = 0; i < visit_dates.length; i++) {
    if (visit_dates[i] != 0) {
        last_date = visit_dates[i];
    }
}

4. Using the moment.js function, we convert the last visit date to the date format.

var last_visit_date = moment(last_date, 'DD-MM-YYYY');

5. Using isSame  function, we can now check if the last visit date is the same as the end of the study date.

if (end_study.isSame(last_visit_date)){
    "Yes.";
} else {
    "No.";
}
Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Castor Support Team

  2. Posted