This topic is locked
[SOLVED]

 Formatting time field on View as tab

4/20/2016 8:33:36 AM
ASPRunner.NET General questions
Pete K author

I'm having much difficulty with something that should be quite simple. I have a field (StartTime) that is of data type Time in the SQL database. I'm trying to get it display as a short time ("h:mm tt"). If I just select "Time" from the pre-defined formatting options on the view as tab, it displays as 24 hour time and also includes seconds. (By the way, it would be most helpful if ASPR.net supplied some help with formatting dates and times with clickable properties on this tab.)
So I am venturing out into the fraught world of .net string formatting. In this context from what I have read here and on MSDN, I believe the following should work, but when I try it I just get the raw unformatted string.

String.Format("{0:h:mm tt}", Convert.ToDateTime((string)data["StartTime"]))


Can someone help me figure out what I'm doing wrong? The only possible wrinkle I can see is that Convert.ToDateTime expects a string representing a full datetime rather than just time. But according to https://msdn.microsoft.com/en-us/library/xhz1w05e(v=vs.110).aspx the function can handle this situation by assuming the current date if the date is is missing, which is fine for my purposes since I'm not displaying the date anyway.

Pete K author 4/20/2016

Man, do I feel stupid. I had everything right.... except I left out the all-important "value = "

value = String.Format("{0:h:mm tt}", Convert.ToDateTime("10/06/1961 8:35:00"))


Duh!

T
Tim 4/20/2016

Hey Pete,
Glad you got it figured out, but I will say I feel your pain with formatting time in ASPR. I have also struggled with getting it right on the view page, and getting the time control to work properly on the add/edit pages. One solution I use for the view pages is to format the time in the SQL query, and then make that a field type of "text". Here is the way to format time as a 12 hour clock with AM/PM:
select

convert(varchar(100), StartTime, 100) as ViewStartTime

from table
Your approach is good too. I mostly just wanted to "plus 1" your idea of making clickable properties to help with formatting.
Thanks,

Tim

Pete K author 4/20/2016

Thanks, Tim. That's another great approach. Seems there's always more than one way to solve a problem.