Using if/else logic with dates in EDC/CDMS
Table of Contents
You can use Moment.js to compare two or three dates with each other. You can use this to build advanced dependencies or calculations.
How to start
More information about how to create a Moment.js object out of a field in Castor can be found here: Doing date calculations: the basics.
var date = moment('{inclusionDate}', 'DD-MM-YYYY');
Compare two dates
Two dates can be compared using the following functions:
Function | Represents logic symbol |
isBefore | < |
isSame | == |
isAfter | > |
isSameOrBefore | <= |
isSameOrAfter | >= |
All functions work in the same way. The syntax for using the function is
momentObject1.functionName(momentObject2)
Examples of date comparisons
Example 1. Check if the visit date (visitDate) is before the inclusion date (inclusionDate).
var inclusion = moment('{inclusionDate}', 'DD-MM-YYYY');
var visit = moment('{visitDate}', 'DD-MM-YYYY');
if(visit.isBefore(inclusion)) {
'The visit date is invalid, please check the date.';
}
This will compare the two dates based on milliseconds. To compare the two dates on another unit (year, month, week, day, hour, minute, or second), you can add another parameter to the function, which is also shown in Example 2.
momentObject1.functionName(momentObject2, 'precision')
Example 2. Check if the lab date (labDate) is not in the week as the visit date (visitDate).
var visit = moment('{visitDate}', 'DD-MM-YYYY');
var lab = moment('{labDate}', 'DD-MM-YYYY');
if(!lab.isSame(visit, 'week')) {
'The lab date is not in the same week as the visit date, please check the date.';
}
Compare three dates
It is also possible to compare three dates.
Function | Represents logic symbol |
isBetween | < && |
The syntax for using the isBetween function is
momentObject1.isBetween(momentObject2, momentObject3)
Example 1. Check if the lab date (labDate) is not in between the admission (admissionDate) and discharge date (dischargeDate).
var admission = moment('{admissionDate}', 'DD-MM-YYYY');
var discharge = moment('{dischargeDate}', 'DD-MM-YYYY');
var lab = moment('{labDate}', 'DD-MM-YYYY');
if(!lab.isBetween(admission, discharge)) {
'The lab date is not in between the admission and discharge date,
please check the date.';
}