格式化datetime对象

时间:2013-10-01 14:04:56

标签: asp.net datetime

我有一个我在GridView中显示的dateTime字段。问题是我只需要显示系统的日期而不是时间,但它显示日期和时间。应该采用什么样的格式?

这会在文本框中设置日期:

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

        }
    }

从表中检索:

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;






    }

gridView.aspx代码:

 <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>

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:0)

您可以使用ToString()格式化DateTime,格式字符串是根据以下选项创建的: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")

等等。

修改

@JohnH正确地指出,在使用GridView的特定上下文中,您不会使用ToString或其他方法,而是提供DataFormatString属性。我将为格式化日期的一般用例和包含可用格式字符串的MSDN资源的链接留下我的答案。