Skip to content

Calculate the Time Difference in Hours Between Two DateTime Fields

Following video demonstrates using a JavaScript to calculate the time difference in hours between two datatime fields and assign it to another number field.

https://youtu.be/Tw3xbwOfMeM

Following script is used in the above video,

// Get time difference in hours
const from = <insert from field>; //From date
const to = ; <insert to field>//To date
 
const fromDate = new Date(from);
const toDate = new Date(to);
// Calculate the time difference in milliseconds
const timeDifference = toDate - fromDate;
 
// Convert the time difference from milliseconds to hours
const timeDifferenceInHours = timeDifference / (1000 * 60 * 60);
 
<insert destination field> = timeDifferenceInHours;