Canvas Apps Expressions

Converting a date time from 24 hour to 12 hour format

Date time fields in Power Apps are displayed, by default, in a 24 hour format. I was working on a project a few days ago where the customer wanted the time displayed in 12 hour format both while creating a new record and updating/viewing an existing record.

A date time field, by default, looks like this:

Date time field in 24 hour format (click to enlarge)

To convert this to a 12 hour format, follow the following steps:

  • Unlock the date time data card
  • Add a new dropdown to be used for AM/PM to the date time field's data card
  • Set the Items property of this dropdown to
    ["AM", "PM"]
  • Set the Default property of this dropdown to
    If(Hour(Parent.Default) <12, "[$-en-US]AM", "PM")
  • Change the Items property of the Hour dropdown to
    ["00","01","02","03","04","05","06","07","08","09","10","11","12"]
  • Change the Default property of this dropdown to
    Text(If(Mod(Hour(Parent.Default),12) = 0, 12, Mod(Hour(Parent.Default), 12)), "[$-en-US]00")
  • Change the Update property of the data card to
    DateValue1.SelectedDate + Time(If(AMPMValue1.Selected.Value = "PM", 12, 0) + Mod(Value(HourValue1.Selected.Value), 12), Value(MinuteValue1.Selected.Value), 0)

With these changes, the form looks like this (I added another data card to compare the time in 24 hour format with the 12 hour format):

Date time field in 12 hour format (click to enlarge)

I then updated the time from 8 PM to 9 PM:

Updated date time field in 12 hour format (click to enlarge)

To summarize, the above steps not only help in displaying a date time field of an existing record in 12 hour format, but it also let's you update the date time field for an existing record and also at the time of creating a new record.

Have fun! Get addicted!

2 thoughts on “Converting a date time from 24 hour to 12 hour format”

Leave a Reply