How to convert Time in AM or PM

Hi,
I was looking for the option to get the Time to be converted from 24 Hrs. format to 12 Hrs format along with AM or PM, so that the same can be displayed in in the mobile device through displaying text (Label or Text).
I was referring the URL : Calculations with Dates and Times - CommCare Public - Dimagi Confluence

But, i am not getting the correct syntax.

Any further suggestion ?

Thanks and regards,
Gurudayal Sahu

Hi Gurudayal,

I think we might not have a function that would give you time in 12 hours format, but you can apply simple maths to get the same.
Here are the steps

  • Create a hidden value hour_24 which would use the format-date function and save the current hour in 24-hour format. Use this calculate condition:
format-date(now(), '%H')
  • Create another hidden value hours_12 which would convert the above 24-hr format to 12-hr format. Use this calculate condition:
if(#form/hour_24 > 12, #form/hour_24 - 12, if(#form/hour_24 = 0, 12, #form/hour_24))
  • Create another hidden value am_pm to store the am or pm text. Use this calculate condition:
if(#form/hour_24 > 11, "PM", "AM")
  • Finally in your label's display text, simply use these hidden values to show the current time in 12-hr format. Assuming you'd want to show time in this format "HH:MM AM/PM" then simply add this to your label's display text:
Today: <output value="#form/hours_12" />:<output value='format-date(now(), "%M")'/>  <output value="#form/am_pm" />

Thanks, Shivam !

It worked. This is very helpful !

Thanks and regards,
Gurudayal Sahu