Converting Military time to 12 hour time

Mike Bilz asked on August 14, 2017 19:16

Hello Kentico Team,

I am attempting to turn military time into standard 12 hour time.

My idea was to check if the number in the hours case is greater than 12, and if so to subtract twelve from that number and add a "pm" at the end.

So I attempted to write the following: take the time field from the XML, convert it to a string, split it on the colon and keep the first half, convert that to an integer and check if it is larger than the number 12.

<%# If((Eval("DMEETING_TIME_START").ToString().Split((":")[0]).ToInt() > 12), ""+(Eval("DMEETING_TIME_START").ToString().Split((":")[0]).ToInt() -12)+":"+Eval("DMEETING_TIME_START").ToString().Split((":")[1])+"pm", ""+Eval("DMEETING_TIME_START")+"am") %>

So far this hasn't worked.

'System.Array' does not contain a definition for 'ToInt' and no extension method 'ToInt' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

I may very well be over complicating this whole idea. Can anyone tell me what I got wrong in the script above, or clue me in to an easier way to handle this conversion?

Thanks in advance for your help.


Correct Answer

Brenden Kehren answered on August 14, 2017 19:28

Use the transformation method

FormatDateTime(Eval("DMEETING_TIME_START"), "HH:mm tt")

It will format the input based on the current culture and the string.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Mike Bilz answered on August 14, 2017 22:12

Thank you, Brenden! That was exactly what I was looking for.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.