I am pulling in date values from a sql server database using a gridview and and the date gets converted from
12/12/2009 to 12/12/2009 12:
set the dataformatstring value to "{0:d}"
Ex:
<asp:BoundField HeaderText="Date" DataField="Date_Field" ReadOnly="True" DataFormatString="{0:d}">
</asp:BoundField>
Try the below code:
<asp:BoundField DataField="my_date" HeaderText="Date"
ReadOnly="True" SortExpression="my_date"
DataFormatString="{0:d}" />
In the above mentioned code, my_date
is the date column of the sqlserver table. The DataFormatString="{0:d}"
is the main portion of this code to resolve the particular issue of yours.
Within
asp:Label runat="server" Text='<%# Eval("DateAndTime") %>'
Try adding "{0:M-dd-yyyy}"
asp:Label runat="server" Text='<%# Eval("DateAndTime", "{0:M-dd-yyyy}") %>'
You can use the ToString()
method with a mask:
ToString("MM/dd/yyyy");
UPDATE: Just realized it would be easier in your case to do this in the grid view template
<asp:BoundField DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />
You can use a DataAnnotations attribute and a DynamicField control; then you don't have to do the same formatting every time you want to format that field. There is an example showing how to do this here: http://www.asp.net/entity-framework/tutorials/the-entity-framework-and-aspnet-%E2%80%93-getting-started-part-8
You can also use .ToShortDateString()
on the DateTime Object if you are already manipulating the date in the RowDataBound