How can I calculate the time difference?

  • abccare
    Asked on June 8, 2017 at 12:04 PM

    Hi,

     

    I have quite a complex form, which I need to show the full difference between two dates & times. I've split it into another form to practice, and I've tried to follow various topics on here but I can't seem to find one that works.

    I have a date & time out field and a date & time in field. I need to know the Days, Hours & minutes difference.

    Are you able to provide guidance?

     

    Thanks,

    Chris

  • Kiran Support Team Lead
    Replied on June 8, 2017 at 2:00 PM

    You may calculate the difference of the dates using the subtraction directly. However, the time difference cannot be calculated using the date field. You may consider using the time field separately to calculate the difference of the time. 

    Please give it a try and let us know if you need any further assistance. We will be happy to help. 

  • abccare
    Replied on June 9, 2017 at 5:38 AM

    Is there any work around for this? It seems a bit odd to have the date & time fields but no way to calculate the difference between them?

  • Support_Management Jotform Support
    Replied on June 9, 2017 at 7:08 AM

    I'm currently looking for a workaround that involves the Date Field alone with the Time option enabled within it. I'll update you on this same thread as soon as I have some news.

  • Support_Management Jotform Support
    Replied on June 10, 2017 at 8:02 AM

    Thanks for waiting. From your test form, here are all the things I gathered:

    - It has to be a Date Picker Field WITH the Time Option enabled

    - The difference should be denominated into days > hours > minutes

    It took a while but I managed to come up with a workaround that you may want to check out. Here's the form: https://www.jotform.com/71592504701957 

    A total of 9 Form Calculation Widgets were used. The first 6 widgets should have been hidden but I purposely displayed them for demo purposes.

    1. Date Difference to Days

    Formula: (Check Out date - Check In date) * 86400

    2. Days converted to Hours

    Formula: Date Difference / 3600

    3. Days (temp)

    Formula: Days converted to Hours / 24

    4. Decimal part of Days

    Formula: Days (temp) - floor (Days(temp))

    Why would we need to get the decimal part of the Days (temp) field? What we're hoping to get here is the remainder from the division done on formula #2 above. Since there's no native way to fetch the remainder using Form Calculation, I ended up using the decimal part of the quotient to convert it into a remainder.

    The floor() function was used to get the round down the floating point result.

    For example, given that:

    Date Difference = 2746800

    Days converted to Hours = 763

    Days (temp) = 31.79

    Decimal part of Days = 0.79

    The remainder of 763 divided by 24 is 19.

    To get 19 as a result, just multiply the Decimal part of Days to the divisor (which is 24).

    So, 0.79 x 24 = 18.96 (rounded off to 19).

    5. Hours (temp)

    Formula: Decimal part of Days * 24

    6. Minutes (temp)

    Formula: ((Days converted to Hours - floor (Days converted to Hours)) * 60

    The same idea was applied to the Minutes (temp) field. Use floor to round down the floating point number, get its decimal place then convert it back to a remainder.

    7. Days

    Formula: floor (Days (temp))

    8. Hours

    Formula: Hours (temp)

    This field was set to not show decimal places

    9. Minutes

    Formula: Minutes (temp)

    This field was set to not show decimal places

    If this works for you, I strongly recommend you clone my form.

    Complete guide: How-to-clone-an-existing-form-from-a-URL 

    If this still doesn't meet your requirements, I'm afraid the only choice you have is to use the Date Picker and Time Fields independently. Afterwards, calculate them separately.

  • abccare
    Replied on June 10, 2017 at 12:15 PM

    Perfect! This is what I was after! Thank you!