I’m not sure if this question has been asked before but I can’t seem to find a suitable answer in the forum.
I have a Date Question in my questionnaire which allow the user to pick a date from the drop down calendar. This could be any previous date including today, but not a future date. I’ve tried to add the following validation to my Date question but it is not accepted, DateTime.Now > self.
Is there a way to validate my Date question against the current Date?
- Current date is captured with a date-type question with a flag to capture current time.
Capture the time of the visit at the beginning of the interview. Use it as an anchor point and calculate all time-dependent values relative to that date.
Accessing DateTime.Now is intentionally prohibited to prevent difficult to track errors in questionnaire design.
- You wrote “I’ve tried to add the following validation to my Date question but it is not accepter, DateTime.Now>self”
If you tried it, then you should have obtained this exact compilation error message “[WB0118]:Expression referencing forbidden DateTime properties: DateTime.Now, DateTime.UtcNow, DateTime.Today. Consider using current time question instead.”
Please cite the error message exactly in the queries. If you think the error message is not clear, how in your opinion can we make it more transparent?
Thank you for your reply and yes, I’m aware of your approach with the current time flag on the Date question, however, I was hoping to stay away from adding extra questions on the form to capture the current time and use that as the comparison base for other date questions, rather I was hoping that there might be a system date variable that could be used in validations through code without the need to capture the current date.
In the absence of
DateTime.Now, a partial, imperfect work-around: hard-code what constitutes a future dates.
If your survey ends on or before date X, prohibit dates beyond X. If you know broadly when interviews will occur and can preload some information, preload the expected date of interview, and prohibit dates beyond the interview-specific date of interview.
For my part, I still favor Sergiy’s solution of capturing the current date via the current date-time question.
I think it is important to understand that in Survey Solutions an interview could be reopened multiple times, for example, after rejections. Recording any system generated current date might then result in an ambiguous behavior. That is why a user-defined approach is clearer for user interpretation. You know what you mean by the current date.