How can you display the date in a gridview as a single date rather than a datetime?

asp.net entity-framework gridview

Question

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:00:00 AM

How do I prevent that from happening ?

Thanks !

1
22
8/4/2011 4:59:59 AM

Accepted Answer

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}" />
44
4/9/2016 9:50:20 AM

Popular Answer

You can set the date format in the bound column like this

<itemtemplate>
<asp id="Label1" runat="server" Label.Text='<%# Bind("YourDateField", "{0:M-dd-yyyy}") %>'>;
</asp>
</itemtemplate>


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow