Calculations with time field type

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}

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 for details.

1 Like

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.

@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.

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

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"/>

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

Day0_04_OPR_test.xlsx (16.1 KB)
@dicksonsamwel See attached

Hi @Dan, see the modified excel

Day0_04_OPR_test (1).xlsx (11.9 KB)

1 Like

Thanks so much @dicksonsamwel.

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 !

2 Likes