Diferença entre datas e horas

  • VieiraJose
    Perguntado em 22 de maio de 2024 às 19:32

    Ola!

    Eu gostaria de ajuda para realizar a seguinte função:

    Calcular a diferença entre inicio do serviço (DD/MM/AAA HH:MM) e final do serviço (DD/MM/AAA HH:MM), onde essa diferença seja exibida em dias e horas.

    Exemplo: início do serviço 21/05/2024 10:10
    final do serviço 22/05/2024 14:00

    a fórmula deve me mostra: 1 dia 3 horas e 50 minutos ou então 27 horas e 50 minutos.


    Obrigado.

  • Mike_G Jotform Support
    Respondido em 22 de maio de 2024 às 22:07

    Hi Jose,

    Thanks for reaching out to Jotform Support. Unfortunately, our Portuguese Support agents are busy helping other Jotform users at the moment. I'll try to help you in English using Google Translate, but you can reply in whichever language you feel comfortable using. Or, if you'd rather have support in Portuguese, let us know and we can have them do that. But, keep in mind that you'd have to wait until they're available again.

    Now, coming back to your question, it is possible to calculate the date and time difference on the form and show the result in days, hours, and minutes format. I have created a demo form. You can find it here. Feel free to test and clone it to your account so you can inspect it. Click here to see how to clone the form.

    You might also want to check out this guide about Performing Date and Time Calculations.

    The fields in my demo form require time input in a 12-hour format. I understand you want it in a 24-hour format. I’ll need a bit of time to work out a solution. I’ll get back to you as soon as I can.

    In the meantime, let us know if you have any other questions.

  • Mike_G Jotform Support
    Respondido em 26 de maio de 2024 às 06:53

    Hi Jose,

    Thanks for your patience and understanding, we appreciate it. After investigating a bit, I found that the same calculation I have with my Demo Form here would work if we change the time format of the date fields with the time field enabled to 24-hour format. I understand you want to add the date and time difference calculation and display its result on your AES - ORDEM DE SERVIÇO MS (240943101080039). Let me show you how to set it up:

    Adding a hidden field to calculate the difference between the two date and time fields's value:

    1. On your My Forms page, hover your mouse over your AES - ORDEM DE SERVIÇO MS form and click on Edit Form on the right side of the screen.
    2. In Form Builder, click on the Add Form Elements menu on the left side of the page.
    3. Under the Widgets tab, search for Form Calculation and drag and drop it to your form.
      Note: 
      Click on the Wand icon on the right to open Form Calculation Widget Settings if it does not open automatically after adding the widget to the form.
    4. In the Widget Settings, use the Add Field button above the Numpad on the right to add the Data e Hora da abertura da O.S. field and the Previsão de início da atividade field to the calculation box on the left.
    5. In the calculation box, put a minus sign between the date fields in the formula.
      Note:
       You can type the Minus key on your keyboard or click on it from the numpad on the right of the calculation area.
    6. Click on Save.Diferença entre datas e horas Image 1 Screenshot 200
    7. Next, click on the Gear icon to open its Properties.
    8. Go to the Advanced tab, enter Raw Date Difference in the Field Label property, and toggle the Hide property to the On position.Diferença entre datas e horas Image 2 Screenshot 211

    Adding a hidden field to round down the raw date difference field's value to the nearest integer to get the exact number of days:

    1. Click on the Add Form Elements menu again on the left side of the page.
    2. Under the Widgets tab, search for Form Calculation and drag and drop it to your form.
      Note: 
      Click on the Wand icon on the right to open Form Calculation Widget Settings if it does not open automatically after adding the widget to the form.
    3. In the Widget Settings, add the floor() function to the calculation box on the left and use the Add Field button to add the Raw Date Difference field within the function's parenthesis.
      Note:
       You can get the floor() function by clicking on the fx formula icon on the numpad.
    4. Click on Save.Diferença entre datas e horas Image 3 Screenshot 222
    5. Next, click on the Gear icon to open its Properties.
    6. Go to the Advanced tab, enter Days in the Field Label property, and toggle the Hide property to the On position. Optional: Toggle the Shrink property to the On position.Diferença entre datas e horas Image 4 Screenshot 233

    Adding a hidden field to calculate the difference between the raw date difference field's value and the number of days field's value multiplied by 24 to get the raw hours and minutes.

    1. Click on the Add Form Elements menu again on the left side of the page.
    2. Under the Widgets tab, search for Form Calculation and drag and drop it to your form.
      Note: 
      Click on the Wand icon on the right to open Form Calculation Widget Settings if it does not open automatically after adding the widget to the form.
    3. In the Widget Settings, use the Add Field button above the Numpad on the right to add the Raw Date Difference field and the Days field to the calculation box on the left.
    4. Put a minus sign in the formula in the calculation box between the Raw Date Difference field and the Days field, enclose everything in parenthesis, and add an asterisk and the 24 after.
      Note:
       You can type the Minus key, Parenthesis, and Asterisk on your keyboard or click on them from the numpad on the right of the calculation area.
    5. Click on Save.Diferença entre datas e horas Image 5 Screenshot 244
    6. Next, click on the Gear icon to open its Properties.
    7. Go to the Advanced tab, enter Raw Hours and Minutes in the Field Label property, and toggle the Hide property to the On position. Optional: Toggle the Shrink property to the On position.Diferença entre datas e horas Image 6 Screenshot 255

    Adding a hidden field to round down the raw hours and minutes field's value to the nearest integer to get the exact number of hours:

    1. Click on the Add Form Elements menu again on the left side of the page.
    2. Under the Widgets tab, search for Form Calculation and drag and drop it to your form.
      Note: 
      Click on the Wand icon on the right to open Form Calculation Widget Settings if it does not open automatically after adding the widget to the form.
    3. In the Widget Settings, add the floor() function to the calculation box on the left and use the Add Field button to add the Raw Hours and Minutes field within the function's parenthesis.
      Note:
       You can get the floor() function by clicking on the fx formula icon on the numpad.
    4. Click on Save.Diferença entre datas e horas Image 7 Screenshot 266
    5. Next, click on the Gear icon to open its Properties.
    6. Go to the Advanced tab, enter Hours in the Field Label property, and toggle the Hide property to the On position. Optional: Toggle the Shrink property to the On position.Diferença entre datas e horas Image 8 Screenshot 277

    Adding a hidden field to round 60 multiplied by the difference between the raw hours and minutes field's value and its value rounded down to the nearest integer to get the exact number of minutes:

    1. Click on the Add Form Elements menu again on the left side of the page.
    2. Under the Widgets tab, search for Form Calculation and drag and drop it to your form.
      Note: 
      Click on the Wand icon on the right to open Form Calculation Widget Settings if it does not open automatically after adding the widget to the form.
    3. In the Widget Settings, use the fx formula icon to add the round() function to the calculation box on the left. 
    4. Within the round() function's parenthesis in the formula, multiply 60 by the raw hours and minutes field subtracted by the raw hours and minutes field within a floor() function.
    5. Click on Save.Diferença entre datas e horas Image 9 Screenshot 288
    6. Next, click on the Gear icon to open its Properties.
    7. Go to the Advanced tab, enter Minutes in the Field Label property, and toggle the Hide property to the On position. Optional: Toggle the Shrink property to the On position.Diferença entre datas e horas Image 10 Screenshot 299

    Adding a field to show the final day(s), hour(s), and minute(s) difference of the two date fields on the form:

    1. Click on the Add Form Elements menu again on the left side of the page.
    2. Under the Widgets tab, search for Form Calculation and drag and drop it to your form.
    3. Note: Click on the Wand icon on the right to open Form Calculation Widget Settings if it does not open automatically after adding the widget to the form.
    4. In the Widget Settings, use the Add Field button to add the Days, Hours, and Minutes fields to the calculation. After each field, type " day(s), ", " hour(s), and ", and " minute(s)" respectively.
    5. Click on Save.Diferença entre datas e horas Image 11 Screenshot 3010
    6. Next, click on the Gear icon to open its Properties.
    7. Go to the Advanced tab, and enter Final Days, Hours, Minutes Difference in the Field Label property.Diferença entre datas e horas Image 12 Screenshot 3111

    Important:

    For the Raw Date Difference, Days, Raw Hours and Minutes, Hours, and Minutes fields that we added, set their decimal places to 10. Let me show you how to do it:

    1. Click on the Wand icon on the right to open Form Calculation Widget Settings.
    2. In Widget Settings, click on the Options button on the right, below the Numpad.Diferença entre datas e horas Image 13 Screenshot 3212
    3. On the next screen, click on the dropdown on the left of the Decimal Places in Numerical Results option, select 10 in the menu, and click on Go Back.Diferença entre datas e horas Image 14 Screenshot 3313
    4. Click on Save.Diferença entre datas e horas Image 15 Screenshot 3414

    As for the Final Days, Hours, Minutes Difference field, set it to read-only. Let me show you how to do it:

    1. Click on the Wand icon on the right to open Form Calculation Widget Settings.
    2. In Widget Settings, click on the Options button on the right, below the Numpad.Diferença entre datas e horas Image 16 Screenshot 3515
    3. On the next screen, uncheck the box on the left of the Result field is read only option and click on Go Back.Diferença entre datas e horas Image 17 Screenshot 3616
    4. Click on Save.Diferença entre datas e horas Image 18 Screenshot 3717

    And that's it. Check out the screencast below to see the result:

    Diferença entre datas e horas Image 19 Screenshot 3818

    I've cloned your form and made the necessary adjustments so you can test it before applying the changes to yours. You can find my cloned form here. Feel free to clone my form to your account so you can inspect it. Click here to see how to clone the form.

    Give it a try, and reach out again if you have any other questions.