Calculate minutes between start and end time

By: David Burley | Asked: 11/10/2024
ForumsCategory: How-toCalculate minutes between start and end time
David Burley asked 1 month ago
I have two fields a start time and end time using Formidable Time fields How do I calculate the difference in minutes between them in my form and then use that calculated result elsewhere in the form on another field calculation
3 Answers
Best Answer
David Burley answered 1 month ago
I have taken https://formidableforms.com/knowledgebase/javascript-examples/#kb-calculate-time-difference this snippet and changed the keys to match what I have, but my duration in minutes field isn't updating when I go from field to field, do I need to have a submit button for this to work
Victor Font replied 1 month ago

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?

Victor Font replied 1 month ago

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.

David Burley replied 1 month ago

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

David Burley replied 1 month ago

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 ?

David Burley replied 1 month ago

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

David Burley answered 1 month ago
yes I changed the key IDs
Victor Font replied 1 month ago

Where did you insert the jQuery? Have you checked the browser console for jQuery errors.

David Burley replied 1 month ago

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 ?

David Burley answered 1 month ago
getting this in the console on a test page:-   The specified value "2:00" cannot be parsed, or is out of range   this is where I had selected 02:00 and 04:00 as the start and end time   and The specified value "6:00" cannot be parsed, or is out of range.   when I selected 06:00 and 12:00
Victor Font replied 1 month ago

I answered this comment higher up in the thread.

Rob LeVineRob LeVine replied 1 month ago

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?

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right