Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript PDF JS to calculate time difference in hours and minutes

RedBeard

New Coder
Got some AI to slap this together and it isn't working. I've tinkered with different languages but its not more than a vague familiarity.

JavaScript:
// Define the custom calculation script for the Total field
var alertField = this.getField("Alert");
var inServiceField = this.getField("InService");
var totalField = this.getField("Total");


// Calculate the time difference between Alert and In Service fields
function calculateTimeDifference() {
    var alertTime = alertField.value;
    var inServiceTime = inServiceField.value;


    // Parse the time strings into Date objects
    var alertDate = util.scand("h:MM tt", alertTime);
    var inServiceDate = util.scand("h:MM tt", inServiceTime);


    // Calculate the time difference in milliseconds
    var timeDifference = inServiceDate.getTime() - alertDate.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 Total field with the calculated difference
    totalField.value = hours.toString() + " hours " + minutes.toString() + " minutes";
}


// Set the calculation script to trigger when either Alert or In Service changes
alertField.setAction("Calculate", calculateTimeDifference);
inServiceField.setAction("Calculate", calculateTimeDifference);
 
Last edited by a moderator:
Got some AI to slap this together and it isn't working.
This is not unexpected when copying and pasting code generated by AI.

With the information you've provided, it's impossible for me to tell what the problem is.
Do you know why it's not working? Are there any errors in the console?
I assume this isn't the complete code. As far as I know, there's no getField() method on the global window object, which this refers to in the global scope. Are there any libraries you're using?
 
Back
Top Bottom