Validation condition with two date + time pickers

I have a form where 3 date + time pickers question are given and I want to use validation condition to make sure the consequent answers are always later in time ( time left < time of arrival < time finished).

I have tried already to put it plain in the validation conditions which did not work
and I did try to use double() and double(date()) options as suggested in the topic below (and the suggested link):

Does someone has suggestions to get this validation condition?

Hi Jos,

Does your validation condition look something like this?

#form/time_left < #form/time_of_arrival < #form/time_finished

I’m not certain, but I wouldn’t be surprised if that format isn’t supported. Instead, I’d try:

#form/time_left < #form/time_of_arrival and #form/time_of_arrival < #form/time_finished

  • Ethan

HI Ethan,
Thanks for the suggestion. To clarify I indeed need ‘time_left < time_of_arrival’ for second entry and ’ time_of_arrival < time_finished’ for the last question
This works I think for the day picker, but appears not to work for the day and time picker.

Jos, to compare dates and times, you may need double(), as you mentioned in your first link, though if you use date(), it will drop the time information, so the comparisons may be off. Could you paste in the exact expression you’re using?

Hi Jos,

This has a lot more nuance to it that makes comparing time questions very error prone. Please see the answer to this question for more context:

https://forum.dimagi.com/t/getting-the-difference-between-time-questions-inside-a-repeat-group/5525/2

Yes I am aware of the nuances of this type of comparison, as we are in UTC and non daylight saving area I can accept the risk. And would prefer to keep the date for the occasional difference of dates.
As from the documentation and links provided above I would expect to get a decimal from the epoch with after the ‘.’ the day fraction. I made a test app to visualize the results. tl = time_left ar = arrival both are date and time picker questions with same date but different time:
normal = unaltered
string = string()
double = double()
double_date = double(date())

I would expect to see time info at normal, but I do not see any. With using data export tool to database I actually do see the time info, so the info is there.
At double I would expect to get a decimal with time info but it shows an integer, meanwhile with the double(date) option I would expect it to cut of to whole days (integer) but shows a decimal instead, but the time info is the same meanwhile entered times are different.
To me I do not see the logic of this behaviour.

Thanks for the demonstration, I agree that that behavior is unintuitive. That last option appears to do what you need, does that not compare properly? That is, does this give inaccurate results?

double(date(tl)) <= double(date(ar))

I indeed initially expected it to work, but when I had a closer look it appears the “time info” is showing the same meanwhile there is two hour difference in my example. ~06:15 and ~08:15

Small addition: my goal in this case is just to validate the dates are sequential (= to improve data input) the duration calculations I can easily do on the database side.

I just tested it out and observed the same behavior you’ve described. As you’ve mentioned, “date and time” questions are stored properly for exports, but don’t seem to be well suited for calculations. It might be easier to work around this issue by instead using two questions, one for the date, and a second for the time. tl-date and tl-time, for example. Then for validation, you can compare dates, or if the dates are the same, compare the times. Something like:

if(date(tl-date) = date(ar-date), tl-time < ar-time, tl-date < ar-date)

For export, you can combine the date and time components into a datetime stamp:

concat(/data/question1, ‘T’, /data/time)

  • Ethan

Hi Ethan,
Thanks indeed I think you are right I might have to look for something like that. I will discuss the options with persons who are actually using the app and see which option we prefer.
The main reason I need the validation is the UI of the date and time picker as by swiping to the next question one could easily change the input.