Select to view content in your preferred language

Formatting Time into 12 Hour with AM/PM

1484
4
Jump to solution
04-03-2024 10:09 AM
INTRNSRYAN
New Contributor

Hello,

I am concatenating a training name with a date and time.  I want the time to be in 12 hour format with AM/PM on the end.  Can someone help me out?

Here is the code I have so far:

concat(${training_idname}, ' - ', concat((format-date(${training_date}, '%m%d%Y')),' @ ', (format-date(${start_time}, '%H:%M'))))

 

Thanks,

Ryan 

0 Kudos
1 Solution

Accepted Solutions
JenniferAcunto
Esri Regular Contributor

You can calculate it based on the 24 hr time.  

if(format-date(${date1}, '%h') >12, (format-date(${date1}, '%h')-12), if(format-date(${date1}, '%h') = 0, '12', format-date(${date1}, '%h')))

Here  I pull the hour and if it's greater than 12 I subtract 12 from it. Midnight is treated as 0, so if the hour is 0 I change it to 12.

Next I need a calculation to determine AM/PM. 

if(format-date(${date1}, '%h') >11, 'PM', 'AM')

If the hour is greater than 11, it is PM, otherwise use AM. It's important to use the original date field as opposed to the hour field you just calculated as we are using the 24 hour format to determine the time of day.

Now I just  need to put it all together.

concat(${hour1}, ':', ${min1}, ' ', ${ampm})

 

JenniferAcunto_0-1712233886806.png

 

- Jen

View solution in original post

4 Replies
JenniferAcunto
Esri Regular Contributor

You can calculate it based on the 24 hr time.  

if(format-date(${date1}, '%h') >12, (format-date(${date1}, '%h')-12), if(format-date(${date1}, '%h') = 0, '12', format-date(${date1}, '%h')))

Here  I pull the hour and if it's greater than 12 I subtract 12 from it. Midnight is treated as 0, so if the hour is 0 I change it to 12.

Next I need a calculation to determine AM/PM. 

if(format-date(${date1}, '%h') >11, 'PM', 'AM')

If the hour is greater than 11, it is PM, otherwise use AM. It's important to use the original date field as opposed to the hour field you just calculated as we are using the 24 hour format to determine the time of day.

Now I just  need to put it all together.

concat(${hour1}, ':', ${min1}, ' ', ${ampm})

 

JenniferAcunto_0-1712233886806.png

 

- Jen
INTRNSRYAN
New Contributor

Thanks Jennifer!

0 Kudos
DeborahDennison
Emerging Contributor

Hi Jennifer, Thank you for sharing the calculations for the date/time formats!  I am trying to figure something similar, but with using only the 'time' type (rather than dateTime).  Would you be able to help with another calculation for 'time' so it shows AM or PM? 

With Appreciation,

Debbie 

0 Kudos
JenniferAcunto
Esri Regular Contributor

If using the field app, the above should work. If using the browser you need to use substring to pull the first two characters for the hour and subtract 12 if it is greater than 12.

 

if(substr(${time1}, 0, 2) > 12, int(substr(${time1}, 0, 2))-12, substr(${time1}, 0, 2))

 

 Next pull the separator and minutes. 

 

substr(${time1}, 2, 5)

 

Determine if it is AM or PM.

 

if(substr(${time1}, 0, 2) > 12, ' PM', ' AM')

 

 Combine them all.

 

concat(${hour1}, ${min1}, ${ampm})

 

 

Ignore the preview in Connect, as it treats it as a date object and so it will not be correct. The brower treats it as a string object, so once you publish and test in the browser it will look correct.

 

JenniferAcunto_1-1717416797475.png

 

- Jen
0 Kudos