Start a conversation

Calculate time difference in seconds (using text fields)

Time fields in Castor only capture hours and minutes. If you want to capture time in the format HH:mm:ss, you need to use text fields. However, you can calculate with those as well!


To calculate the difference between two text fields where you are capturing time as HH:mm:ss, you can use the following formula:

var timePoint_1 = '{time1}';
var timePoint_2 = '{time2}';
var split_1 = timePoint_1.split(':');
var split_2 = timePoint_2.split(':');
var date1 = new Date(0, 0, 0, split_1[0], split_1[1], split_1[2]); 
var date2 = new Date(0, 0, 0, split_2[0], split_2[1], split_2[2]);
var timeDifference = new Date(date2 - date1);
var diffInSeconds = Math.floor(timeDifference/1000);
diffInSeconds;

Replace time1 and time2 with your own variable names for time and leave the rest as it is.


Check format of time

In addition, you can also use a calculation to check if the formatting of the time that has been entered is correct (since it is free text, you might want to check this) and render a message. For that you can use the following formula:

var time1 = "{time1}";
var time2 = "{time2}";
if (!time1.match(/\d\d:\d\d:\d\d/) || !time2.match(/\d\d:\d\d:\d\d/)) {
    "The previous field does not match the required HH:mm:ss format.";
} else {
    "The previous field was formatted correctly";
};
Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Tonya

  2. Posted
  3. Updated