Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • Contact Us
  • Home
  • CDMS
  • Castor CDMS Calculations Manual
  • Calculations with date and time

Doing date (and time) calculations in EDC/CDMS: the basics

Contact Us

If you still have questions or prefer to get help directly from an agent, please submit a request.
We’ll get back to you as soon as possible.

Please fill out the contact form below and we will reply as soon as possible.

  • CDMS
    Castor CDMS Manual Castor CDMS Calculations Manual Frequently Asked Questions Articles for Data Managers Castor CDMS Compliance Release Documents
  • eConsent
    Castor eConsent Manual Castor eConsent Compliance Release Documents
  • SMS
    Castor SMS Manual Castor SMS Compliance Release Documents
  • Castor Connect
    Castor Connect Compliance Release Documents Castor Connect Manual Castor Connect - Participant Quick Start Guide
  • Helpdesk
    News Other Resources Castor products knowledge resources
  • Status page
  • Completing a Study
+ More

Table of Contents

Creating a Moment object Add or subtract days/months/years Calculate the difference between dates Format dates Further examples

Calculation fields in Castor are often used to build advanced date validations. In Castor dates are stored as string (text) in the format 'DD-MM-YYYY'. It is generally necessary to indicate this in your calculations. For this you can use the so-called 'Moment' function in JavaScript. Moment.js is a JavaScript library that offers many possibilities to validate, calculate with, and display dates and times.

Creating a Moment object

For example, you want to calculate using your date field called 'inclusionDate'. You can create a Moment object from this variable with:

 

var date = moment('{inclusionDate}', 'DD-MM-YYYY');

This tells the system that the variable {inclusionDate} is in essence text (hence the quotation marks), stored in the format 'DD-MM-YYYY'. A Moment object is created from the variable, meaning it 'transforms' it into an actual date. In this case, 'date' is your Moment object. This name is irrelevant, you can call it whatever you like, but the 'var' up front has to stay as it is (indicating new variable).

 

You can also similarly use a date and time field. This field uses the 'DD-MM-YYYY HH:mm' format. The following example shows how you can create a Moment object from the variable 'labSample_time’.

 

var datetime = moment('{labSample_time}', 'DD-MM-YYYY HH:mm');

As soon as you have saved the date or date and time in the appropriate Moment object, you can use this object to further perform calculations (in the same calculation field).

 

Add or subtract days/months/years

Example 1. Add 7 days to a date field ('admission')

var admission = moment('{admission}', 'DD-MM-YYYY');
var newDate = admission.add(7, 'days');
newDate.format('DD-MM-YYYY');
  1. In the first line, the Moment object is created for the field 'admission'.
  2. Seven days are added to the object, which is stored in the variable 'newDate'
  3. The new date is shown in the right format

Example 2. Subtract 6 months from a date field ('discharge')

var discharge = moment('{discharge}', 'DD-MM-YYYY');
var newDate = discharge.subtract(6, 'months');
newDate.format('DD-MM-YYYY');

Calculate the difference between dates

Most date calculations in Castor are used to calculate the difference between two date fields or two date and time fields. See the article Calculate the difference between two dates for more examples of calculating differences between dates and times.

 

Example 1. Calculate the difference in days between two dates ('visit1’ and 'visit2’).

var visit1 = moment('{visit1}', 'DD-MM-YYYY');
var visit2 = moment('{visit2}', 'DD-MM-YYYY');
visit2.diff(visit1, 'days');

 

Example 2. Calculate the difference in minutes between two datetime fields ('measurement1’ and 'measurement2’).

var m1 = moment('{measurement1}', 'DD-MM-YYYY HH:mm');
var m2 = moment('{measurement2}', 'DD-MM-YYYY HH:mm');
m2.diff(m1, 'minutes');

 

Example 3. Calculate the difference between two dates ('date1’ and 'date2’) with 2 decimals.

var measurement1 = moment('{date1}', 'DD-MM-YYYY');
var measurement2 = moment('{date2}', 'DD-MM-YYYY');
var years = measurement2.diff(measurement1, 'years', true);
years.toFixed(2);

 

This example calculates the difference in years. If you look at the third line, you can see that 'true' disables rounding. In the last line the number of years is shown with 2 decimals.

Format dates

 

Example 1. Show the date in the ddd. D MMMM, YYYY format, e.g. Wed. 17 May, 2017:

var admission = moment('{admission}', 'DD-MM-YYYY');
admission.format('ddd. D MMMM, YYYY');

 

Example 2. Calculate the difference between two dates and display it as text.

var measurement1 = moment('{date1}', 'DD-MM-YYYY');
var measurement2 = moment('{date2}', 'DD-MM-YYYY');
var diff = measurement2.diff(measurement1);
var humanized = moment.duration(diff).humanize();
'The measurements were taken ' + humanized + ' after each other';

 

The difference is "humanized", meaning that it is shown as text.

Further examples

Check out the other articles in the manual as well as our Form Exchange for other, ready-to-use date calculation templates.

edc/cdms time calculation

Was this article helpful?

Yes
No
Give feedback about this article

Related Articles

  • Using if/else logic with dates in EDC/CDMS
  • Subtract a month from a date in EDC/CDMS
  • Extract only date or time from date and time field in EDC/CDMS
  • Calculations with Number & Date Field
ISO 27001
FDA - 21 CFR part 11
ICH GCP compliant
HIPAA compliant
CDISC
ISO 9001
gdpr compliant

Products & Industries

  • Electronic Data Capture (EDC)
  • ePRO
  • eConsent
  • Decentralized Clinical Trials (DCT)
  • Clinical Data Management
  • Medical Device & Diagnostics
  • Biotech & Pharma
  • CROs
  • Academic Research

Resources

  • Thought Leadership
  • Blog
  • Castor Academy
  • Knowledge Base

 

Company

  • About Us
  • Careers
  • News
  • Contact Support
  • Contact Us

Legal & Compliance

  • Terms of Use
  • Privacy & Cookie Statement
  • Responsible Disclosure Policy
  • Good Clinical Practice (GCP)
  • ISO Compliance Certificates
  • GDPR & HIPAA Compliance
  • Security Statement

© 2022, Castor. All Rights Reserved.

Follow us on social media


Knowledge Base Software powered by Helpjuice

Definition by Author

0
0
Expand