Power Apps Validation - End Date And Time Should Be Greater Than Start Date And Time

Overview

In this article, we will talk about how we can apply DateTime validation in our Power Apps Form. Most of the time, we need to implement one most common validation in Power Apps where End Datetime must be greater than Start DateTime. How can we apply that validation? Let’s see!

We have the following form in our Power Apps. We want to validate, check-out time should be greater than check-in time! So, how can we achieve this?

Step 1

Please check the Datacard for Check-In and Check-Out time.

Step 2

Add the following line of code on the below highlighted controls. We need to add the same code on all following controls.

If(
    Or(
        IsBlank(
            DateValue2.SelectedDate + Time(
                Value(HourValue2.Selected.Value),
                Value(MinuteValue2.Selected.Value),
                0
            )
        ),
        IsBlank(
            DateValue1.SelectedDate + Time(
                Value(HourValue1.Selected.Value),
                Value(MinuteValue1.Selected.Value),
                0
            )
        )
    ),
    Set(
        IsEndDateValid,
        true
    ),
    DateDiff(
        DateValue1.SelectedDate + Time(
            Value(HourValue1.Selected.Value),
            Value(MinuteValue1.Selected.Value),
            0
        ),
        DateValue2.SelectedDate + Time(
            Value(HourValue2.Selected.Value),
            Value(MinuteValue2.Selected.Value),
            0
        ),
        Minutes
    ) > 0,
    Set(
        IsEndDateValid,
        true
    ),
    Set(
        IsEndDateValid,
        false
    )
)

Step 3

Set Validation message,

If(!IsEndDateValid, "Ënd Date Time should be greater than Start Date Time")

Step 4

Set Visible property for an Error message.

Step 5

Prevent form for submitting an invalid entry by setting up the below condition for “OnSave”. If you are using a standalone Canvas app, then you need to write the same code on your button click.

If(IsEndDateValid ,SubmitForm(SharePointForm1))

Step 6

Change OnNew Property of form. If you are using a standalone Canvas app, then you need to write the same code on the New button of the form.

NewForm(SharePointForm1);Set(IsEndDateValid,true)

Step 7

Save and Publish form!

Conclusion

This is how we can simply apply end DateTime validation for Power Apps! Isn’t that amazing?

Happy Power Apping!!