Emerald
Emerald

Reputation: 874

DataFormatString for time (ASP.Net)

I tried to re-format the time displayed in GridView by only showing HH:mm. Currenly my time displayed is HH:mm:ss. Coding as follow.

<asp:BoundField HeaderStyle-CssClass="thStyle" DataField="time" HeaderText="Time Upload" />

I tried DataFormatString="{0:HH:mm}" but when I run the system it threw me an error which is

Input string was not in a correct format.

Thank you.

Upvotes: 2

Views: 8828

Answers (2)

Philip Stratford
Philip Stratford

Reputation: 4733

The answer by @Emerald got me there even though I don't think it's quite right - it at least has a couple of typos. This worked for me:

[DisplayFormat(DataFormatString = "{0:hh\\:mm}", ApplyFormatInEditMode = true)]

I didn't need the HtmlEncode=false, but if you find yourself wanting to try it note that that false should not be in quotation marks. The ApplyFormatInEditMode isn't necessary for the formatting to work but in my experience you want it set to true more often than not.

Upvotes: 2

Emerald
Emerald

Reputation: 874

Manage to get the HH:mm using HtmlEncode="false" DataFormatString="{0:hh\:mm}"

Upvotes: 6

Related Questions