Answered by:
DataFormatString to display hours ...

Question
-
User1720127711 posted
Hi all,
I was wondering how I can format values in a time property to show only hours. I've seen you can use format strings like this:
[DisplayFormat(DataFormatString = "{0:HH:mm:ss}")]
However, when I use this kind of strings, I have an error when I try to open "list" pages:
System.FormatException: Input string was not in a correct format.
Of course I can just use:
[DisplayFormat(DataFormatString = "{0:t}")]
It is just I want to have more options.
Thanks and bye ...
Monday, February 25, 2013 6:07 PM
Answers
-
User281315223 posted
You should take a look at the Custom Date and Time Formatting page on MSDN, which covers basically every type of formatting that you can perform to DateTime objects.
You should also take a look to ensure that your input is in the correct format that is expected (if it is not, that will explain your "Input is not in the correct format error"). You mentioned that this occured when you were displaying lists, which means that one or more of your Date fields may be in the incorrect format or possibly null.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 25, 2013 6:14 PM
All replies
-
User281315223 posted
You should take a look at the Custom Date and Time Formatting page on MSDN, which covers basically every type of formatting that you can perform to DateTime objects.
You should also take a look to ensure that your input is in the correct format that is expected (if it is not, that will explain your "Input is not in the correct format error"). You mentioned that this occured when you were displaying lists, which means that one or more of your Date fields may be in the incorrect format or possibly null.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 25, 2013 6:14 PM -
User-1716253493 posted
look like your data that you want to display is not correct DateTime or nullMonday, February 25, 2013 6:15 PM -
User1929428035 posted
try {0:d}
Monday, February 25, 2013 6:43 PM -
User220175380 posted
Hi,
Try below URL
http://www.codeproject.com/Questions/261952/How-to-get-only-hours-from-a-date-in-asp-net
Monday, February 25, 2013 11:04 PM -
User-1716253493 posted
Set directly to the control property
text='<%# Eval("yourfield","{0:HH}" %>'
Tuesday, February 26, 2013 4:02 AM -
User1720127711 posted
Hi, and many thanks for all your answers.
I'm using the new Time data type for this column in Sql Server 2008; and the column is declared as Not Null; so, every value in this table should be correct. For some reason, DD does not like some acceptable string formats for this Time column. For example, if I use this string suggested by Oned:
[DisplayFormat(DataFormatString:="{0:HH}")] (I'm of course using this in an attribute for the property as is documented for DD)
I get the same error as above:
Input string is not in a correct format.
If I use:
[DisplayFormat(DataFormatString:="{0:t}")]
I don't have an error *but* get this format instead: 09:35:00; however, I'm looking for something like this: 9:35 am.
Do you think there'd be an error somewhere when DD tries this Time data type in Sql Server 2008. I should say I'm using VS 2012 and the type Entity Framework shows for this column is Time.
Thanks again for your answers.
Bye ...
Tuesday, February 26, 2013 9:10 AM -
User2027663642 posted
Try:
DataFormatString = @"{0:hh\-mm}"
Friday, January 8, 2016 4:29 AM -
User-330204900 posted
Hi manuelrivera, is this a Dynamic Data question? are you using one of the DD Project types? if so then it is the DataFormat attribute you would use and specifically
[DisplayFormat(DataFormatString = "{0:HH}")]
Pretty much as you said only the string is formatted the way you asked.
Friday, January 8, 2016 11:27 AM