All Collections
Form Builder
Form Scripting & Calculations
Calculating Duration from two Time Fields
Calculating Duration from two Time Fields
Angela Lim avatar
Written by Angela Lim
Updated over a week ago

You can use our Lua scripting language to calculate the duration between two time fields in a form result.

First, you'll need to make sure that your form is setup with the correct field types.

The field you'd like to use to compare time should be a Time Entry field (not a Text field). 

Make sure that each Time field that you'd like to calculate on has a slug.

The duration between the two times will need to be added to a Numeric value field. Once you have added a numeric field to your form, open up the settings to the field. 

The Numeric field format can either be a Number or a Duration. If the format is a Number, the duration between the two time fields will be calculated in total minutes. If the format is Duration, the duration will be calculated in hours and minutes. Select the format that you'd like to use for your form.

Add a slug to the duration field, then click Save Changes.

Once you have your fields set up in your form, you can add the calculation script to the Scripting box. To calculate the duration between two time fields, you can enter:

answers.duration = answers.end_time - answers.start_time 

Make sure to replace the duration , end_time  and start_time  with the slugs you've assigned in your fields. 

Click Save Changes.

Any new form results will now calculate the duration once both time fields have been entered!

Did this answer your question?