Date Format in GridView ASP.NET 2.0
One of the common objectives in projects is to display date in M-DD-YYYY format in GridView. The GridView may be bound dynamically from a database, XML, CSV, WebService or any other data source.
Let us take a small snippet to solve this problem at a go:
<asp:GridView ID="dgVolunteers" runat="server" SkinID="GridTheme" Width="100%" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField HeaderText="Member Since" DataField="TimeStamp"
DataFormatString="{0:M-dd-yyyy}" />
</Columns></asp:GridView>
This will print date like "5-06-2008"
However, if the need of the hour is to display the time as well, add an attribute HtmlEncode=“false” to asp:BoundField. There is no change in the way in which you bind a data source to your grid using C# or VB.NET.
Here are some of the string formats that you can try out:
Specifier | type | output (June 8, 1970 12:30:59) |
---|---|---|
dd | Day | 08 |
ddd | Short Day Name | Mon |
dddd | Full Day Name | Monday |
hh | 2 digit hour | 12 |
HH | 2 digit hour (24 hour) | 12 |
mm | 2 digit minute | 30 |
MM | Month | 06 |
MMM | Short Month name | Jun |
MMMM | Month name | June |
ss | seconds | 59 |
tt | AM/PM | PM |
yy | 2 digit year | 70 |
yyyy | 4 digit year | 1970 |
: | seperator, e.g. {0:hh:mm:ss} | 12:30:59 |
/ | seperator, e.g. {0:dd/MM/yyyy} | 08/06/1970 |