Select to view content in your preferred language

Restricting users from entering future dates in survey

635
4
04-04-2024 11:20 AM
WorcesterGTSS
Frequent Contributor

I have a survey that will be for the public that will allow them to enter a date but I would like to restrict dates that can be entered to the current date or earlier. Based on the answer to this previously asked question, using the today() function will not consistently work. I have tried using format-date to get the current date and the entered date in a consistent format and then subtract the entered date from the current date. The constraint would be that if the result is a negative value, it would a future date and thus not accepted. I'm able to get the date conversions to work correctly but I can't get the constraint part to work. I've added a Calculate type field and have the calculation as 

${converted_current_date} - ${converted_date_of_sighting}

I've added .<0 to the constraint field for the Calculate field. When I test it, any date I enter receives the constraint message.

I'm new to Survey123 Connect and I'm not sure what I'm doing wrong - is the logic of my constraint faulty? Do I have the constraint in the correct field?

Survey123.jpg

 

 

 

0 Kudos
4 Replies
JohnPlunkett
Esri Contributor

Hello,

Have you tried this approach?

JohnPlunkett_1-1712262154864.png

 

JohnPlunkett_0-1712262136792.png

.<= today()

 

Reference

https://doc.arcgis.com/en/survey123/desktop/create-surveys/xlsformformulas.htm

 

Constraints

Adding a constraint to a survey question restricts the accepted inputs for a response. This can include a specific range of numbers, combinations of letters and numbers, or general pattern matching. In your spreadsheet, the constraint expression is entered into the constraint field and informational text is entered into the constraint_message column of the survey worksheet. In the constraint expression, the input for the question is always represented by a period.

For example, you can use the following formula to restrict the input of an integer field to positive numbers only:

.>= 0

This formula, when applied to a date field, prevents the user from entering a value earlier than today:

.>= today()

You can also use calculations in constraints. This formula performs a calculation to only allow the user to select dates between today and 14 days from today:

(.>= today()) and (.<=(today() + (1000 * 60 * 60 * 24 * 14)))

0 Kudos
WorcesterGTSS
Frequent Contributor

Hello,

As I stated in my post, a different Esri Community thread indicates that using today() in such a manner may not yield desired results. From the response in that thread by @JamesTedrick:

The today() function stores a value representative of local 12:00 noon for the date in question.  If you are trying to compare a whole date, I would suggest using format-date() to extract the day, month and year components and compare with the day, month and year of the date you are comparing.

Is that no longer the case?

Thank you.

0 Kudos
RobertAnderson3
MVP Regular Contributor

What I might do is use the today() plus time to get it to be like midnight for the day, similar to John's solution above:

(.<=(today() + (1000 * 60 * 60 * 12)) which adds 12 hours to today()

Or what I do formatting my dateTime (though this might be extra for your use because this came in when I started using Power Automate and needed to pass the time through it and I was having timezone issues) is:

format-date(${ServiceDate},'%Y-%m-%d')+"T23:59:59 [Halifax]"

I use this to set a dateTime field in my survey to the ServiceDate (which is selectable by the user, but defaults to today() and I can't imagine why you can't just plug that in directly) at 11:59pm.

The [Halifax] is for the Atlantic Timezone, I prefer this instead of the -03:00 for daylight savings reasons (as far as I can tell it does it right this way). I'm guessing for Eastern time for Mass (great state) it'd be [New York]. Or you might be able to use the pulldata("@property",'utcOffset') in the calculation instead. 

WorcesterGTSS
Frequent Contributor

Thanks for the ideas! I'll give them a try.