• Resolved fibbu

    (@fibbu)


    Hello, I was wondering how I can create an equation like this:

    I want to have 2 criteria:

    1. Criterion 1: Calculate the time difference between two times (HH:mm).
    2. Criterion 2: Calculate the time difference between two date+time inputs (e.g., DD.MM.YYYY HH:mm).

    Additionally, depending on the user’s selection:

    • If the user selects Criterion 1, only the start and end time inputs should appear and be calculated.
    • If the user selects Criterion 2, start and end date+time inputs should appear and be used in the calculation.

    I’ve been trying some scenarios but haven’t been able to get it to work.
    Would you be able to help me?

    Thank you.

    • This topic was modified 1 year, 1 month ago by fibbu.
Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author CodePeople2

    (@codepeople2)

    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!

    Plugin Author CodePeople2

    (@codepeople2)

    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?

    Plugin Author CodePeople2

    (@codepeople2)

    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.
    Plugin Author CodePeople2

    (@codepeople2)

    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.

    Thread Starter fibbu

    (@fibbu)

    thanks

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Time Difference calculator’ is closed to new replies.