web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Logic to set DateTime ...
Power Apps
Answered

Logic to set DateTime variable based on dropdowns

(0) ShareShare
ReportReport
Posted on by 87

I have a date and time picker that I'm trying to use to set a global variable varStartTime. When PM is selected, I would like to advance the time by 12 hours so that it reflects PM. However, I am experiencing a bug I just cannot wrap my head around. Here is my code in the OnSelect and OnChange of all of the controls:

 

 

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

 

 

Here is my date and time picker:

 

elbent_0-1699636783057.png

 

Below the time is the global variable in a text box for testing. As you can see, the variable has a PM value, even though the dropdown shows AM. It only happens when the hour field is set to 12. I'm missing something obvious and I've looked at it too long so any help is appreciated. Thanks.

Categories:
  • M_Ali_SZ365 Profile Picture
    1,110 on at

    Hi @elbent ,

    Try This Code

    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)),
     Value(StartTimeMinute.SelectedText.Value),
     0
     )
    );

    If this solution resolves your issue, please consider marking it as accepted.

    Warm regards,
    Muhammad Ali

  • elbent Profile Picture
    87 on at

    Unfortunately that didn't solve it. It doesn't seem to be an issue with the values, it's with the logic

  • Verified answer
    M_Ali_SZ365 Profile Picture
    1,110 on at

    @elbent 

    I understand the issue you're facing. It seems that the logic for adjusting the time based on the "AM" or "PM" selection in the dropdown is not working as expected when the hour field is set to 12. To address this, you can modify the logic as follows:

    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, // Special case for 12 AM
    Value(StartTimeHour.SelectedText.Value)
    )
    ),
    Value(StartTimeMinute.SelectedText.Value),
    0
    )
    );

    This updated code checks if the selected time is "AM" or "PM" and if the hour is 12. If it's "PM," it adds 12 hours to the time, and if it's "AM" and the hour is 12, it sets the hour to 0 to represent 12 AM.

    If this solution resolves your issue, please consider marking it as accepted.

    Warm regards,
    Muhammad Ali

  • elbent Profile Picture
    87 on at

    Thank you so much, this worked! appreciate it.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 411 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 338

#3
WarrenBelz Profile Picture

WarrenBelz 256 Most Valuable Professional

Last 30 days Overall leaderboard