Javascript to calculate the date and time difference between 2 text fields with Default Data being [time]

By: Rhett Finck | Asked: 02/22/2024
ForumsCategory: How-toJavascript to calculate the date and time difference between 2 text fields with Default Data being [time]
Rhett Finck asked 9 months ago
To whom it may concern, Is there a way I can calculate the time difference between two text fields with default value being [time] I need to calculate the hours worked between 2 fields. I can also change them to Time fields if necessary.   Your assistance in this regard is sincerely appreciated    
2 Answers
Victor Font answered 9 months ago
Try this: https://www.w3resource.com/javascript-exercises/javascript-date-exercise-45.php
Luis Gerardo Macareno Balderas replied 9 months ago

Hi Victor, i was testing this, but never works, never returns a value 🙁
https://formidableforms.com/knowledgebase/frm_validate_field_entry/#kb-calculate-total-time

Victor Font replied 9 months ago

My instructions to the OP are to use jQuery/JavaScript. But you've chosen to use a WordPress hook that doesn't return a calculated value to the front end.

I don't even understand why Formidable chose the frm_validate_field_entry to show this example. It's the wrong filter to use. The example code doesn't do anything except set values to a list of variables, but nothing in that code makes use of any calculations.

If you want to see the results in real time on the front end, you need to use jQuery, Formidable's hooks fire on the server, not in the browser.

If you want a calculation that's done by PHP and have it returned to the front end, you have to do this in jQuery Ajax. Alax requires a jQuery component and a PHP callback function that returns the calculated value.

If you use jQuery for the entire project, performance will be better becaue everything is handled in the browser and there are no round trips to the server.

Luke M answered 9 months ago
This code (link) should work for you. Just follow the intructions and the calculation will be done after submit. Use iQuery if you`d like to see the result before submit (remove "frm_hidden" class on calculation field). You could also do some math in a view (even dynamically).

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