Select to view content in your preferred language

Calculating hours and minutes in Survey 123 Connect

314
1
02-25-2025 12:15 PM
SueBoelk2
Occasional Contributor

I am a real  rookie using Survey 123 and especially w/ the calculations (but I am not giving up)!

I used a formula that I researched to calculate and output hours and minutes:

(However, I don't know how to show the colon between the hours and minutes after running the form.

concat(int((${t2} - ${t1}) div (1000*60*60)), ":", if(int(((${t2} - ${t1}) div (1000*60)) mod 60) < 10,"0",""), int(((${t2} - ${t1}) div (1000*60)) mod 60))

The formula is quite long but I can hide some of the fields that I don't want to see.)

Or even if I am using the correct expression.

Please help w/'simple' if possible 🙂

Any help is appreciated.

Thanks.

0 Kudos
1 Reply
DavidPike
MVP Notable Contributor

Maybe another way is shown here, Solved: Calculate time between two date-times using a date... - Esri Community

and broken down into more bite-sized chunks:

 

 

#Considering your dates are epoch times, they represent the 
#number of seconds #since 1970.  
#Esri use milliseconds. so:

#ms_diff is the milliseconds (ms) between the two dates.
ms_diff = ${endtime} - ${starttime}

#there are 60,00 ms in a minute,
#3,600,000 ms in an hour.

decimal_hours = ${ms_diff} div 3600000
#round down using int
rounded_hours = int(${decimal_hours})

decimal_mins = ${ms_diff} div 60000
#round down using int
rounded_mins = int(${decimal_mins})

# rounded_mins is your total minutes difference between 
#the two datetimes,
# if you use it alongside hours, you need to subtract 
#the rounded hours*60 to get the minutes remainder.
#or take the modulus as below:
modulo_mins = ${rounded_mins} mod 60

#H, M padding and logic to pad a 0 if Hrs <10
times = if(${rounded_hours} < 10, '0' + ${rounded_hours}, ${rounded_hours}) + 'H:' + if(${modulo_mins} < 10, '0' + ${modulo_mins}, ${modulo_mins}) + 'M'

#I'd set all to visible to see what's going on and trial a few time #differences in the app, then set appearance field to 'hidden' as needed #after.

 

 

typenamelabelreadonlycalculation
dateTimestarttimestarttime  
dateTimeendtimeendtime  
decimalms_diffms_diffyes${endtime} - ${starttime}
decimaldecimal_hoursdecimal_hoursyes${ms_diff} div 3600000
integerrounded_hoursrounded_hoursyesint(${decimal_hours})
decimaldecimal_minsdecimal_minsyes${ms_diff} div 60000
integerrounded_minsrounded_minsyesint(${decimal_mins})
integermodulo_minsmodulo_minsyes${rounded_mins} mod 60
texttimestimesyesif(${rounded_hours} < 10, '0' + ${rounded_hours}, ${rounded_hours}) + 'H:' + if(${modulo_mins} < 10, '0' + ${modulo_mins}, ${modulo_mins}) + 'M'

 

 

0 Kudos