Answered
Custom calculation script for finding time difference not running.
Didnt write this myself, I directed an AI to create it using ECMAScript 5 (ES5) for compatibility with the 11 Pro edition. When I put this into the custom calc area of the field properties nothing happens. I have tried trouble shooting with syntax checkers and the like and found nothing.
// Define the custom calculation script for the text field
var time1Field = this.getField("Alert");
var time2Field = this.getField("InService");
var resultField = this.getField("Total");
// Function to parse a time string into a Date object
function parseTime(timeString) {
var timeParts = timeString.split(":");
var hours = parseInt(timeParts[0], 10);
var minutes = parseInt(timeParts[1], 10);
// Create a new Date object with the current date and parsed time
var currentDate = new Date();
currentDate.setHours(hours);
currentDate.setMinutes(minutes);
currentDate.setSeconds(0); // Optional: Set seconds to 0 if not needed
return currentDate;
}
// Calculate the difference between Time1 and Time2
function calculateTimeDifference() {
var time1 = time1Field.value;
var time2 = time2Field.value;
// Parse the time strings into Date objects
var date1 = parseTime(time1);
var date2 = parseTime(time2);
// Calculate the time difference in milliseconds
var timeDifference = date2.getTime() - date1.getTime();
// Convert the time difference to hours and minutes
var hours = Math.floor(timeDifference / (1000 * 60 * 60));
var minutes = Math.floor((timeDifference % (1000 * 60 * 60)) / (1000 * 60));
// Update the result field with the calculated difference
resultField.value = hours.toString() + " hours " + minutes.toString() + " minutes";
}
// Set the calculation script to trigger when either Time1 or Time2 changes
time1Field.setAction("Calculate", function() {
calculateTimeDifference();
});
time2Field.setAction("Calculate", function() {
calculateTimeDifference();
});
