Data,  Functionality

Displaying and saving time in 12 hour AM/PM format

When Power Apps sets up a Date/Time field, the hours are in 24 hour format. Have you ever wanted to work with a 12 hour AM/PM choice and display for the user ? Here is how

Add another drop-down at the right with Items

["AM", "PM"]

and the Default of this drop-down

If(
   Hour(Parent.Default) < 12, 
   "AM", 
   "PM"
)

Then you need to change the Items of the Hour drop-down to

["12","01","02","03","04","05","06","07","08","09","10","11"]

and the Default of the Hour drop-down

With(
   {
      _Hour:
      Mod(
         Hour(Parent.Default),
         12
      )
   },
   Text(
      If(
         _Hour = 0, 
         12,
         _Hour 
      ),
      "00"
   )
)

Now the Update of the Data Card for the Hour dropdown

DatePickerName.SelectedDate + 
Time(
   If(
      AmPmDropdown.Selected.Value = "PM", 
      12, 
      0
   ) + 
   Mod(
      Value(
         HourDropdown.Selected.Value
      ), 
      12
   ), 
   Value(
      MinuteDropdown.Selected.Value
   ),
   0
)

This all should now allow you to perform this function

5 Comments

  • Misty

    Hi Warren. I use your knowledge for reference quite often. In this scenario, the hour updates correctly in the SharePoint list; however, when re-opening the SharePoint Integrated Form, the hour is defaulting to the first value in the Items property. I have followed this to a “T”. Any assistance would be appreciated.

    • Warren Belz

      Hi Misty,
      I assume you are referring to this blog
      The Default of the Hour drop-down

      With(
      {
      _Hour:
      Mod(
      Hour(Parent.Default),
      12
      )
      },
      Text(
      If(
      _Hour = 0,
      12,
      _Hour
      ),
      "00"
      )
      )

      refers to Parent.Default (no real change from the standard except it manipulating the value displayed) so is based on whatever that is in the Data Card. It would normally be ThisItem.YourDateTimeFieldName.
      You could change that to SharePointIntegration.Selected.YourDateTimeFieldName to be more direct with the value, but it should not be necessary.

Leave a Reply

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