function adjust_total() { // On the change event of the start and end times, change the total time worked var total_label = "#field_total_extra1"; var start_time = $("#field_start_time1").val(); var end_time = $("#field_end_time1").val(); var break_time = $("#field_break1").val(); end_time = end_time.slice(0,5); start_time = start_time.slice(0,5) if(start_time !="" && end_time !=""){ console.log("Original end time = " + end_time); // HERE start_time = start_time.split(":"); end_time = end_time.split(":"); var starttime_hours = start_time[0]; var starttime_mins = start_time[1]; var endtime_hours = end_time[0]; var endtime_mins = end_time[1]; var startDate = new Date(0, 0, 0, starttime_hours, starttime_mins, 0); var endDate = new Date(0, 0, 0, endtime_hours, endtime_mins, 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); numb = hours + (minutes/60) - (break_time/60); $(total_label).val(numb); $(total_label).change(); } }
I'm assuming that you are not using Formidable's time field to capture the times. Your image looks like a standard drop down, not a Formidable time field. Formidable time fields use 3-selects per field for Hours, minutes, and AM/PM.
Your code is doing exactly what it's programmed to do. Slice is supposed to be used for splitting arrays. not text strings. With text strings, slice works like substr() and takes the first 5-characters of the field's value. The first 5 characters of "2:00 PM" is "2:00 ". That's the result of your splice. You are not testing for AM/PM. Where are you converting it to 14 before you reach the HERE line?
If you're going to use dropdowns instead of time fields, I suggest you use separate values in your dropdowns. Show the labels as 12-hr. time with the AM/PM indicators and set the value to 24-hr. notation.
An alternative may be to manually create the times in a drop-down field and then changed the saved value of the drop-down to the "minute" equivalent offset from 12am. Then you can use built in shortcode calculations to calculate the time worked and not have to use custom code.
Example:
Start: 8am = 8 (8 hours offset from 12am) *60 = 480 for the saved value in minutes
End: 2pm = 14 (14 hours offset from 12am) *60 = 840 for the saved value in minutes
Break: 60 minutes
Total Calculation = (840-480-60)/60 = 5 (5 hrs worked)
This would work well if they don't work beyond midnight.
You must have "show a single time dropdown" checked to display the field as you have it configured. The field's appearance is irrelevant though. Your Javascript is still incorrect regardless of the way the field displays.
Please login or Register to submit your answer