Create constraint for time

Hello, could anyone help me to develop a constraint for a time spinner input variable? Our survey runs in particular timings 8am-2pm and surveyors have to rotate spinner to input the start time for their observation. To avoid errors in data entry, I want to limit the integers available in AM and PM slots - meaning, digits beyond 2pm and before 8am should not be available in hours slot.

SurveyCTO tool; v5.3; Android tablets

Limiting what the SurveyCTO tool shows in its hours spinner is a bit out of scope for ODK, so you are probably best going to their support site.

But, and assuming SurveyCTO implements compatible XPath expressions, at least in (shameless plug) ODK Collect you could prevent entering an invalid time by using an appropriate constraint on the question, eg

constraint="(format-date-time(., '%h') >= 8) and (format-date-time(., '%h') <= 14)"

See for format-date-time() details.

1 Like

Thank you! Yes, its still not working on SurveyCTO. Tried these also:

  • .>date-time( '%Y-%m-%d 08:%M:%S') and .<date-time('%Y-%m-%d 15:%M:%S')

  • Tried the same with only time function by removing all date related components, that also didn't work.

Hi @guerrilla,

See the attached file to find the time constraint you wanted. The time in files are custom, you can explicitly put the decimal times you want.
TimeConstraint.xlsx (11.9 KB)

Opps, didnt see this. So yes, if you are dealing with times (not dateTimes) then as @dicksonsamwel correctly indicates it would be better to use decimal-time() - not format-date-time() - to perform the necessary comparison; eg

constraint="(decimal-time(.) >= 8/24) and (decimal-time(.) <= 14/24)"

[it would be better to put the actual float value instead of 8/24, but you get the idea]

Although again, I dont know if SurveyCTO actually supports this XPath function either, so YMMV...