Start a conversation

Doing date (and time) calculations: the basics

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. Moment.js makes calculating differences easy.

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.

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Tonya

  2. Posted
  3. Updated