Easily validate Power Apps dates

In this post we’ll see how easily to validate date pickers in Power Apps. Right now am working on an application to help the IT team schedule the deployments for Office ProPlus (Microsoft 365 apps for enterprise) and Teams to users. I’d like to send an email to users asking them if the proposed date is good for them plus other questions that they can deal with in a Power Apps application, if not, they can reschedule. However, I want that when they reschedule for it to be at least 7 days from now so the IT team can have the time to prepare for the deployment and bundle multiple computers together. Also, I want to prevent users from choosing a weekend day. So how to do that? The formula for this would be too short and simple if we understand how to approach it.

First when we choose a date from the picker and click on the submit button, I will have a formula to update a context variable named “isValid”. What I’ll do is, to check if the difference between the selected data and today’s date is less than 7 days. I’ll do that with the DateDiff function, which acts like the following:

Second argument – First argument = Number of days.

This function will return the difference in days between the two dates, so we can check if it’s > 7. Note, this will also validate if the date is in the past as well, since if the date is less than today’s date, the result will be in the minus.

If it’s more than 7 days, we check for weekends. I will be using the Weekday function to determine if the selected date is equal to 1 (Sunday) or 7 (Saturday). To connect all the checks together so they’re all truthy, I’ll be using And operator between all checks, so the final formula would look like this:

UpdateContext({isValid: If((DateDiff(Today(), txtNewDate.SelectedDate, Days) > 7) And (Weekday(txtNewDate.SelectedDate) <> 1) And (Weekday(txtNewDate.SelectedDate) <> 7) , true , false)});       

Few handy functions here and there, to make powerful little formula.

Leave a Reply

Your email address will not be published. Required fields are marked *