Skip to main content
Participant
March 17, 2024
Answered

Custom calculation script for finding time difference not running.

  • March 17, 2024
  • 1 reply
  • 954 views

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();
});

 

This topic has been closed for replies.
Correct answer Nesa Nurani

Try this as custom calculation script of the field where you want to show result:

 

function parseTime(time) {
 var parts = time.split(":");
 var hours = parseInt(parts[0], 10);
 var minutes = parseInt(parts[1], 10);
 return new Date(2000, 0, 1, hours, minutes, 0).getTime();}

var time1Field = this.getField("Alert").value;
var time2Field = this.getField("InService").value;

if (time1Field === "" || time2Field === "") {
 event.value = "";} 
else {
 var timeDifference = Math.abs(parseTime(time2Field) - parseTime(time1Field));
 var hours = Math.floor(timeDifference / 3600000); // 1000 * 60 * 60
 var minutes = Math.floor((timeDifference % 3600000) / 60000); // 1000 * 60
 var hoursLabel = hours !== 1 ? " hours " : " hour ";
 var minutesLabel = minutes !== 1 ? " minutes" : " minute";

 event.value = hours + hoursLabel + minutes + minutesLabel;}

 

1 reply

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
March 17, 2024

Try this as custom calculation script of the field where you want to show result:

 

function parseTime(time) {
 var parts = time.split(":");
 var hours = parseInt(parts[0], 10);
 var minutes = parseInt(parts[1], 10);
 return new Date(2000, 0, 1, hours, minutes, 0).getTime();}

var time1Field = this.getField("Alert").value;
var time2Field = this.getField("InService").value;

if (time1Field === "" || time2Field === "") {
 event.value = "";} 
else {
 var timeDifference = Math.abs(parseTime(time2Field) - parseTime(time1Field));
 var hours = Math.floor(timeDifference / 3600000); // 1000 * 60 * 60
 var minutes = Math.floor((timeDifference % 3600000) / 60000); // 1000 * 60
 var hoursLabel = hours !== 1 ? " hours " : " hour ";
 var minutesLabel = minutes !== 1 ? " minutes" : " minute";

 event.value = hours + hoursLabel + minutes + minutesLabel;}

 

Participant
March 17, 2024

Thank you! It worked perfectly.