Hello @fibbu
You should use conditional statements in the equation to determine the choice selected to return the corresponding result.
For example, if the date/time fields are fieldname1 and fieldname2, and there is a radio button with choices “Criterion 1” and “Criterion 2”. The equation would be simply:
(function(){
let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'y');
if(fieldname3 == 'Criterion 1') {
return diff['hours']+' hours and '+diff['minutes']+' minutes';
}
if(fieldname3 == 'Criterion 2') {
return diff['years']+' years, '+diff['months']+' months, '+diff['days']+' days, '+diff['hours']+' hours and '+diff['minutes']+' minutes';
}
})()
And that’s all.
Best regards.
Thread Starter
fibbu
(@fibbu)
Thank you, it worked — there’s just one missing part.
For example, if the user selects Criterion 1 and enters:
- Start Time: 14:00
- End Time: 08:00
Since the end time is on the next day, the actual time difference is 18 hours, not 6 hours.
How can I structure the logic to handle this correctly — so it automatically assumes the next day if the end time is earlier than the start time?
Thank you!
Hello @fibbu
In that case you are not ignoring the date component as you describe initially, and the diff variable must be calculated into the “if” conditional blocks based on the new requirements. Note the last parameter changes:
(function(){
if(fieldname3 == 'Criterion 1') {
let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'h');
return diff['hours']+' hours and '+diff['minutes']+' minutes';
}
if(fieldname3 == 'Criterion 2') {
let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'y');
return diff['years']+' years, '+diff['months']+' months, '+diff['days']+' days, '+diff['hours']+' hours and '+diff['minutes']+' minutes';
}
})()
Best regards.
Thread Starter
fibbu
(@fibbu)
Form Link
I added the last code you provided.
I selected “Kriter A” (Criterion A) in Fieldname1, set the start time to 14:00 and the end time to 08:00, but the result shows 6 hours.
It should actually be 18 hours, since the end time is on the next day.
Also, it seems like Criterion B is not working at all — no result is shown.
Could you please help me fix this?
Hello @fibbu
If you have date/time components, that’s not possible. Please provide the link to the page containing the form.
Best regards.
Thread Starter
fibbu
(@fibbu)
The correctly working form is here:
🔗 https://onlinealarmkur.com/hours/
And the one we built is this:
🔗 http://www.sgkhizmetbilgisi.com/test?cff-form=6
-
This reply was modified 1 year, 1 month ago by
fibbu.
-
This reply was modified 1 year, 1 month ago by
fibbu.
Hello @fibbu
In your case, where the user cannot access the date component, and the fields order is important, you must compare both values, and if the second field’s value is lower than the first one, you must increase the second value by one day before calculating the difference:
let d1 = fieldname1, d2 = fieldname2;
if(d2<d1){d2 = DATETIMESUM(d2, 'mm/dd/yyyy hh:ii', 1, 'd');}
let diff = DATEDIFF(d1, d2, 'mm/dd/yyyy hh:ii', 'h');
return diff['hours']+' hours and '+diff['minutes']+' minutes';
Please note that the support does not cover implementing the users’ projects. If you need personalized technical support, please contact us directly through the plugin website. Contact Us.
Best regards.