Select to view content in your preferred language

Format time to 12 hour

283
4
a month ago
Labels (1)
leahmaps
Occasional Contributor II

Hi everyone,

I have a survey123 feeding a feature layer that I am using in a web map. Two of the questions are time questions, but they submit to the survey as 24 hour time. I am trying to use arcade to change the format to a 12 hour format, but the expression I think should be working isn't. 

when I use 

Time($feature.StartTime, 'h:mm A')
 
I get the return of null. If I don't have the formatting, I get the 24 hour time returned. Am I doing something wrong, or is there an issue with this function?
0 Kudos
4 Replies
jcarlson
MVP Esteemed Contributor

The Time function is meant to pull the time out of an actual DateTime or Time datatype. If I'm not mistaken, Survey123's Time question type stores a string, so there's nothing for the function to actually work with. You can use Arcade to read the string and modify it to show 12-hour time, though.

// split time by colon
var t = Split($feature['StartTime'], ':')

// get the hour as a number
var the_hour = Number(t[0])

// if the hour is 12 - 23, it's PM, otherwise AM
var am_pm = Iif(
  the_hour >= 12,
  'PM',
  'AM
)

// if the hour is 0, re-write as 12
var hour_str = Iif(
  the_hour == 0,
  '12',
  Text(the_hour, '#')
)

// return the various pieces of the time in one string
return `${hour_str}:${t[1]} ${am_pm}`

 

- Josh Carlson
Kendall County GIS
0 Kudos
leahmaps
Occasional Contributor II

Fingers crossed this will work - just getting the weird issue with arcade having invalid variables. 

leahmaps_0-1716554725069.png

 

0 Kudos
jcarlson
MVP Esteemed Contributor

That's really odd. I use Arcade this way all the time without issues. Does it let to attempt to run the code, or does it insist there's a problem?

Try adding Console(the_hour) above line 8, see if it sends anything to the console.

- Josh Carlson
Kendall County GIS
0 Kudos
jcarlson
MVP Esteemed Contributor

Oh sheesh. I forgot the closing quotation on line 11. Should be 'AM'.

- Josh Carlson
Kendall County GIS
0 Kudos