Skip to main content

Notifications

Power Apps - Building Power Apps
Answered

Logic to display warning if end datetime is before start datetime

(0) ShareShare
ReportReport
Posted on by 87

I'm trying to build custom date and time selectors. I want a warning to display when the end time is before the start time. 

 

elbent_0-1699896367033.png

 

The text boxes in the top left are variables that are set when the selectors are changed. As you can see, the end time is before the start time. In the warning box I have the following in the visible property:

 

If(DateDiff(varStartTime, varEndTime)<0, true, false)

 

I have also tried:

If(DateDiff(DateTimeValue(varStartTime), DateTimeValue(varEndTime))<0, true, false)

 

This is the code that is in the OnSelect and OnChange of the start time selectors:

 

Set(varStartTime, DateTime( Year(DateStart.SelectedDate), Month(DateStart.SelectedDate), Day(DateStart.SelectedDate), If(StartTimeAMPM.Selected.Value = "PM" && Value(StartTimeHour.SelectedText.Value) <> 12, Value(StartTimeHour.SelectedText.Value) + 12, If(StartTimeAMPM.Selected.Value = "AM" && Value(StartTimeHour.SelectedText.Value) = 12, 0, Value(StartTimeHour.SelectedText.Value) ) ), Value(StartTimeMinute.SelectedText.Value), 0 ) );

 

And this is in the end time selectors:

 

Set(varEndTime, DateTime( Year(DateEnd.SelectedDate), Month(DateEnd.SelectedDate), Day(DateEnd.SelectedDate), If(EndTimeAMPM.Selected.Value = "PM" && Value(EndTimeHour.SelectedText.Value) <> 12, Value(EndTimeHour.SelectedText.Value) + 12, If(EndTimeAMPM.Selected.Value = "AM" && Value(EndTimeHour.SelectedText.Value) = 12, 0, Value(EndTimeHour.SelectedText.Value) ) ), Value(EndTimeMinute.SelectedText.Value), 0 ) );

 

 

Why is the warning not showing when the start time is after the end time? Thanks.

  • elbent Profile Picture
    elbent 87 on at
    Re: Logic to display warning if end datetime is before start datetime

    Thank you! I don't know why I didn't just try a simple comparison.

  • Verified answer
    MiDer Profile Picture
    MiDer 130 on at
    Re: Logic to display warning if end datetime is before start datetime

    @elbent 
    The DateDiff expression would give the the difference in return with the unit defined by the 3rd parameter.
    Regardless the position of its first two parameters, thus you will not get a negative value in return.

    You could try the following for your warning box visibility instead:

    If(DateTimeValue(varEndTime) <= DateTimeValue(varStartTime),true,false)

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,343

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,703

Leaderboard