Users fill out a form where they enter what date and time an issue started and when it entered and the form will provide the difference between those times in minutes.
So for example if they enter 4/26/2025 10:00 AM for the start time and 4/26/2025 11:00 AM for the end time, the result of the difference between the two times in minutes would be 60.
This is what I have for the fields the users fill out
HTML
This is what I have for my JavaScript:
function calculateMinutesBetweenDates(date1, date2) {
const timeDifferenceMs = Math.abs(date2.getTime() - date1.getTime());
const minutesDifference = Math.floor(timeDifferenceMs / (1000 * 60));
return minutesDifference;
}
const dateString1 = document.getElementById("StartDateTimeVal").value;
const dateString2 = document.getElementById("EndDateTimeVal").value;
const date1 = new Date(dateString1);
const date2 = new Date(dateString2);
const minutes = calculateMinutesBetweenDates(date1, date2);
console.log(`Minutes between dates: ${minutes}`); // Output: Minutes between dates
However the only result I get is NaN instead of 60 as in the example. It's not giving any other JavaScript errors so not sure why it won't calculate the values as the user sets the date and time fields.
If I set the values directly such as:
const startTime = "2025-04-26T10:00:00";
const endTime = "2025-04-26T11:30:00";
Then it works correctly but need the values to change by user input of the HTML fields.