No. Javascript works in the browser. You do not have to submit. Did you update the code to match your the fields' HTML input ID?
If you copied the code from the KB and only changed the field HTML ids, then you missed at least one. This line is missing the semi-colon at the end: start_time = start_time.slice(0,5)
I'm not sure why you don't see a error specific to that line in the console. start_time will not have a value. Your code editor should have picked up the error, too. Are you using a code editor?
The rest of the code appears to be fine. It's a standard JavaScript date math calculation. As an FYI, you can't do date math directly on the value of a date or time field in JavaScript or PHP. Those fields need to be converted to timestamps where the magic and then converted back to readable text. I have my own functions for testing code and database performance with Formidable. I calculate mine to micro-seconds for performance tuning, and minutes for form display.
so I found the issue causing the console error and it was definitely my fault I hadn't seen of the keys that needed changing. This is now working in that there are no errors but the field still isn't being populated.
So I have the start and end time fields as Time fields is this correct ?
What format should the results field be
Thanks for the assistance btw
fixed it, I had to change the duration results field to be a text field
However, I now get the results in the format HH:MM I need the results in minutes. The code I used was:-
jQuery(document).ready(function($){
$('#field_hlbr1,#field_5v5lu').change(function(){
var start_time = $("#field_hlbr1").val();
var end_time = $("#field_5v5lu").val();
end_time = end_time.slice(0,5);
start_time = start_time.slice(0,5)
if(start_time !="" && end_time !=""){
start_time = start_time.split(":");
end_time = end_time.split(":");
var startDate = new Date(0, 0, 0, start_time[0], start_time[1], 0);
var endDate = new Date(0, 0, 0, end_time[0], end_time[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);
minutes = (minutes < 9 ? "00" : minutes);
console.log(hours + ":"+minutes);
$("#field_j16i2").val(hours + ":"+minutes);
$("#field_j16i2").change();
}
});
});
Any ideas how to modify to just show the results in minutes instead ?
sorted, with the use of ChatGPT 😉
jQuery(document).ready(function($){
$('#field_nht2u, #field_nlr2b').change(function(){
var start_time = $("#field_nht2u").val();
var end_time = $("#field_nlr2b").val();
end_time = end_time.slice(0, 5); // Extract hours and minutes part
start_time = start_time.slice(0, 5); // Extract hours and minutes part
if(start_time !== "" && end_time !== ""){
start_time = start_time.split(":");
end_time = end_time.split(":");
var startDate = new Date(0, 0, 0, start_time[0], start_time[1], 0);
var endDate = new Date(0, 0, 0, end_time[0], end_time[1], 0);
// Calculate the difference in milliseconds
var diff = endDate.getTime() - startDate.getTime();
// Convert milliseconds to minutes
var totalMinutes = Math.floor(diff / 1000 / 60);
// Display total minutes in the input field
$("#field_8ynxl").val(totalMinutes);
$("#field_8ynxl").change();
console.log(totalMinutes + " minutes");
}
});
});
Where did you insert the jQuery? Have you checked the browser console for jQuery errors.
in the after fields section in settings, what format should the results field be as I have it as a numbers field atm. The two other fields for start and end time are in time format.
Is it because I am using the preview functionality and it doesn't work correctly when previewing ?
I answered this comment higher up in the thread.
The fact that it's a number field is likely causing that error because 2:00 is not a number. Can you post a link to the page?
Please login or Register to submit your answer