22

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 !

Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
Mervin
  • 725
  • 2
  • 16
  • 37

10 Answers10

47

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}" />
Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
14

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>
consoleart
  • 151
  • 8
5

set the dataformatstring value to "{0:d}"

Ex:

<asp:BoundField HeaderText="Date" DataField="Date_Field" ReadOnly="True" DataFormatString="{0:d}">
</asp:BoundField>
Vijay
  • 426
  • 1
  • 5
  • 15
4

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}") %>'

Ernest
  • 1,370
  • 13
  • 23
2

You can also use .ToShortDateString() on the DateTime Object if you are already manipulating the date in the RowDataBound

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
1

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

tdykstra
  • 5,880
  • 2
  • 23
  • 20
0

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.

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
0

when you select the field from the database you can convert it to a string in the select as:

convert(varchar, myDate, 101)
Avitus
  • 15,640
  • 6
  • 43
  • 53
-1
for (int j = 0; j < gv_bill_dmd_process_create.Rows.Count; j++)
                {
                    GridViewRow row_fees = (GridViewRow)gv_bill_dmd_process_create.Rows[j];
                    TextBox gv_chk_bill_dept = row_fees.FindControl("txt_gv_DmdProsDuedate") as TextBox;
                    AjaxControlToolkit.CalendarExtender gv_chk_bill_dept1 = row_fees.FindControl("txt_gv_DmdProsDuedate_CalendarExtender") as AjaxControlToolkit.CalendarExtender;
                    gv_chk_bill_dept1.StartDate = fromdate;
                    gv_chk_bill_dept1.EndDate = todate;
                }
-2

use this in query where you are getting date field

CONVERT(VARCHAR,date column name,103) as date

ex:select column1,column2,CONVERT(VARCHAR,date column name,103) as date from tablename

alony
  • 10,725
  • 3
  • 39
  • 46