Vector
Vector

Reputation: 172

Formatting datetime object

I have a dateTime field that i am showing in GridView. The problem is I just need to Show The date of the system And not the time but it shows both date and time. What appropriate formatting should be applied?

this sets the date in textbox:

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBoxdate.Text = DateTime.Today.ToShortDateString();

        }
    }

Retrieves from table:

public static List<Workassign> GetTable()
    {
        List<Workassign> listValues = new List<Workassign>();

        string connectionString = "server=(local);database=project_zeshan;Integrated Security=SSPI";
        SqlConnection con = new SqlConnection(connectionString);

            SqlCommand cmd = new SqlCommand("Select * from Assign_Work", con);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                Workassign ob = new Workassign();
                ob.listItem_1 =rdr["listitem_1"].ToString() ;
                ob.listItem_2 = rdr["listitem_2"].ToString();
                ob.Description = rdr["Description"].ToString();
                ob.Date= DateTime.Parse(rdr["Date"].ToString());
                ob.Image = rdr["Image"].ToString();


                listValues.Add(ob);

            }


            return listValues;






    }

And the gridView.aspx code:

 <form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" ForeColor="#333333" GridLines="None" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="listitem_1" HeaderText="ListItem1" />
            <asp:BoundField DataField="listItem_2" HeaderText="listItem2" />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            <asp:BoundField DataField="Date" HeaderText="Date" />
            <asp:TemplateField HeaderText="Image">
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" Height= "50px" Width = "100px" ImageUrl='<%# Bind("Image") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
</div>
</form>

Upvotes: 0

Views: 74

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239240

You can format the DateTime however you like using ToString() with format string created from the choices here: MSDN - Custom Date and Time Format Strings

// For a standard U.S.-style date:
myDateTime.ToString("MM/dd/yyyy")

// For ISO-style:
myDateTime.ToString("yyyy-MM-dd")

And so on.

EDIT

@JohnH correctly pointed out that in the specific context of working with a GridView, you wouldn't use ToString or other methods, but rather provide a DataFormatString attribute. I'll leave my answer for general use cases of formatting a date and the link to the MSDN resource containing the available format strings.

Upvotes: 0

John H
John H

Reputation: 14655

Try this:

<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:MM/dd/yyyy}" />

Upvotes: 2

Related Questions