# Calculations with time field type

**URL:** <https://forum.getodk.org/t/calculations-with-time-field-type/13038>\
**Category:** Support\
**Created:** [May 10, 2018, 9:43am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038 "2018-05-10T09:43:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dan/32/7019_2.png) [@Dan](https://forum.getodk.org/u/Dan)\
**Post date:** [May 10, 2018, 9:43am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/1 "2018-05-10T09:43:54Z")

</div>

I need my form to calculate the procedure duration and display it as a note after staff have keyed in procedure start and end times. currently I gives me 0 as the output which is not correct. is there something I need to do differently? any suggestions will be appreciated. an excerpt of my form is below

begin group pro\_duration field-list   
time procedure\_start\_time 4. Time the circumcision procedure started (HH:MM:SS): yes   
time procedure\_end\_time 5. Time the circumcision procedure ended (HH:MM:SS): yes   
end group

calculate pro\_dur ${procedure\_end\_time}- ${procedure\_start\_time}  
acknowledge note11 The procedure duration is: ${pro\_dur}

---

<div class="post-metadata">

**Author:** ![Xiphware](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/xiphware/32/2587_2.png) [@Xiphware](https://forum.getodk.org/u/Xiphware)\
**Post date:** [May 10, 2018, 9:33pm UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/2 "2018-05-10T21:33:01Z")

</div>

You probably need to convert your (string) times to decimal times before attempting any arithmetic operations on them. Try something like this:

`decimal-time(${procedure_end_time}) - decimal-time(${procedure_start_time})`

You'll get a real number result representing the minutes fraction of a day; eg 1hr duration (60 minutes) = 0.041  
See [ODK bindings](https://opendatakit.org/help/form-design/binding/) for details.

---

<div class="post-metadata">

**Author:** ![Dan](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dan/32/7019_2.png) [@Dan](https://forum.getodk.org/u/Dan)\
**Post date:** [May 11, 2018, 11:12am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/3 "2018-05-11T11:12:22Z")

</div>

Thanks @Xiphware this solves. I thought it would display the minutes but it doesn't so I divided the value by 1440 to get the duration displayed in minutes. thanks so much for the insight.

---

<div class="post-metadata">

**Author:** ![Dan](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dan/32/7019_2.png) [@Dan](https://forum.getodk.org/u/Dan)\
**Post date:** [May 24, 2018, 6:59am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/4 "2018-05-24T06:59:26Z")

</div>

@Xiphware Sorry to bring this up again but I need a little help still, hope you won't mind. Is it possible to round off a calculate output in the xlsform say to 2 decimal places, or must one have to edit the xml file? Thanks once again.

---

<div class="post-metadata">

**Author:** ![dicksonsamwel](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dicksonsamwel/32/2898_2.png) [@dicksonsamwel](https://forum.getodk.org/u/dicksonsamwel)\
**Post date:** [May 24, 2018, 8:57am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/5 "2018-05-24T08:57:05Z")

</div>

Hi @Dan, there is a round function (`round(xpath/to/value, power)`) you can implement in your form . See the link provided by @Xiphware

---

<div class="post-metadata">

**Author:** ![Dan](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dan/32/7019_2.png) [@Dan](https://forum.getodk.org/u/Dan)\
**Post date:** [May 24, 2018, 9:26am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/6 "2018-05-24T09:26:06Z")

</div>

Hi @dicksonsamwel, Thanks for pointing that out, I had seen the function and tried it out but seemingly I didn't get it right (still on the learning curve with xml) any help will be appreciated. below is the section of the form

```
   <bind nodeset="/Day0_04_OPR_test/pro_duration/procedure_start_time" required="true()" type="time"/>
  <bind nodeset="/Day0_04_OPR_test/pro_duration/procedure_end_time" required="true()" type="time"/>
  <bind calculate="decimal-time( /Day0_04_OPR_test/pro_duration/procedure_end_time ) - decimal-time( /Day0_04_OPR_test/pro_duration/procedure_start_time )" nodeset="/Day0_04_OPR_test/pro_dur" type="string"/>
  <bind calculate=" /Day0_04_OPR_test/pro_dur * 1440" nodeset="/Day0_04_OPR_test/proc_dur" type="string"/>
  <bind nodeset="/Day0_04_OPR_test/note11" type="string"/>

```

---

<div class="post-metadata">

**Author:** ![dicksonsamwel](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dicksonsamwel/32/2898_2.png) [@dicksonsamwel](https://forum.getodk.org/u/dicksonsamwel)\
**Post date:** [May 24, 2018, 11:27am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/7 "2018-05-24T11:27:38Z")

</div>

Hi @Dan, hope someone else may assist, my side i don't do xml. Dont you have the xls form?

---

<div class="post-metadata">

**Author:** ![Dan](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dan/32/7019_2.png) [@Dan](https://forum.getodk.org/u/Dan)\
**Post date:** [May 24, 2018, 11:54am UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/8 "2018-05-24T11:54:48Z")

</div>

[Day0\_04\_OPR\_test.xlsx](https://getodk.b-cdn.net/uploads/default/original/2X/7/720e186c0454c0a8fdb4b60f9b7dd82b7c86698d.xlsx) (16.1 KB)  
@dicksonsamwel See attached

---

<div class="post-metadata">

**Author:** ![dicksonsamwel](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dicksonsamwel/32/2898_2.png) [@dicksonsamwel](https://forum.getodk.org/u/dicksonsamwel)\
**Post date:** [May 24, 2018, 2:18pm UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/9 "2018-05-24T14:18:11Z")

</div>

Hi @Dan, see the modified excel

[Day0\_04\_OPR\_test (1).xlsx](https://getodk.b-cdn.net/uploads/default/original/2X/8/87bbad467d00cb9de08cf9219557e8529cb8274f.xlsx) (11.9 KB)

---

<div class="post-metadata">

**Author:** ![Dan](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/dan/32/7019_2.png) [@Dan](https://forum.getodk.org/u/Dan)\
**Post date:** [May 24, 2018, 2:40pm UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/10 "2018-05-24T14:40:31Z")

</div>

Thanks so much @dicksonsamwel.

---

<div class="post-metadata">

**Author:** ![rachaelb](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/rachaelb/32/9030_2.png) [@rachaelb](https://forum.getodk.org/u/rachaelb)\
**Post date:** [December 5, 2019, 4:58pm UTC](https://forum.getodk.org/t/calculations-with-time-field-type/13038/12 "2019-12-05T16:58:51Z")

</div>

Just a short note to say I was having a similar issue, and delighted to see the solution so helpfully already provided here on the forum. Thank you @dicksonsamwel !
