Cognito Forms – Time Validation Error Message Does Not Dismiss

cognito-forms

I have =DateTime.Now in the Minimum Field to prevent users from entering time in the past. This does prevent past time entry, but the error message "Time must be on or after XX:XXam" does not go away even after a valid future time is entered. Is there a way to get rid of this error message, or is there another way to prevent users from entering time in the past?

I have =DateTime.Today entered in the DATE Field and that works perfectly (it shows error when past date is entered and the error goes away when current or future date is entered).

Best Answer

Working with Time fields in Cognito Forms can be a bit tricky. We intentionally do not support date/time fields yet, as these are affected by timezone and are notoriously difficult to handle correctly and explain. Our Time field is intended to represent a specific time of day, instead of a specific point in time. However, this is in practice impossible to support in calculations, since our calculations are designed to work with dates and date/times, not just times.

All that being said, while DateTime.Now does represent the current date and time, the Time field internally represents the time as a point in time on 1/1/1970. This works just fine when you specify ranges like 8:00 to 5:00 and such, but fails when you use DateTime.Now, which accurately reflects the current date and time.

While I do not like this workaround, you can use the following to specific a time of day based on the current time:

=DateTime.Parse(DateTime.Now.ToString("t"))

This turns the current date and time into a text value that just represents the current time, and then converts the text into a time that is compatible with the time field.