Calculate the difference between two datetime fields: EDC/CDMS
Use the templates below to calculate the difference between two date time fields. Replace datetime1 and datetime2 with your own variable names.
If you want to round your numbers, replace m3 with:
m3.toFixed(X);
with X being the number of decimals. See also: Rounding numbers.
In minutes
var m1 = moment('{datetime1}', 'DD-MM-YYYY HH:mm'); var m2 = moment('{datetime2}', 'DD-MM-YYYY HH:mm'); var m3 = m2.diff(m1, 'minutes'); m3;
In hours
var m1 = moment('{datetime1}', 'DD-MM-YYYY HH:mm'); var m2 = moment('{datetime2}', 'DD-MM-YYYY HH:mm'); var m3 = m2.diff(m1, 'minutes'); m3/60;
In days
var m1 = moment('{datetime1}', 'DD-MM-YYYY HH:mm'); var m2 = moment('{datetime2}', 'DD-MM-YYYY HH:mm'); var m3 = m2.diff(m1, 'minutes'); m3/1440;
Using separate date and time fields
If you are using separate fields for dates and times, use the template below to calculate the difference in minutes:
var m1 = moment('{date1}' + ' ' + '{time1}', 'DD-MM-YYYY HH:mm'); var m2 = moment('{date2}' + ' ' + '{time2}', 'DD-MM-YYYY HH:mm'); var m3 = m2.diff(m1, 'minutes'); m3;