如何在asp.net中更改DataBinder.Eval的日期格式?

时间:2011-10-01 16:09:10

标签: c# asp.net sql-server

我正在试图弄清楚如何更改日期时间格式,以便只显示日期。

            <asp:Repeater ID="RepeaterActions" runat="server">
            <ItemTemplate>
                <li>
                    <span class="historyDate"><%#DataBinder.Eval(Container.DataItem, "ActionListDate")%></span>
                    <span class="historyName"><%#DataBinder.Eval(Container.DataItem, "LeadActionName")%></span><br />
                    <span class="historyNotes"><%#DataBinder.Eval(Container.DataItem, "ActionListNote")%></span>
                </li>
            </ItemTemplate>
        </asp:Repeater>

我猜它在&lt; %%&gt;之间,但我不确定。

我的代码背后是:

<pre>
        protected void RepeaterActionsFill()
    {

        string sql = @"  select a.ActionListDate, a.LeadListID, 
a.ActionListNote, 
l.LeadActionName
from ActionLists as a
INNER JOIN LeadActions as l
ON a.LeadActionID = l.LeadActionID
where a.LeadListID = " + Convert.ToInt32(Request["id"].ToString());

        RepeaterActions.DataSource = DBUtil.FillDataReader(sql);
        RepeaterActions.DataBind();
    }
</pre>

目前,它看起来像这样:

HTML View

我正在寻找的是时间戳去那里。

感谢任何帮助。

编辑:

以下是我要找的内容:

            <asp:Repeater ID="RepeaterActions" runat="server">
            <ItemTemplate>
                <li>
                    <span class="historyDate"><%#DataBinder.Eval(Container.DataItem, "ActionListDate", "{0:M/d/yy}")%></span>
                    <span class="historyName"><%#DataBinder.Eval(Container.DataItem, "LeadActionName")%></span><br />
                    <span class="historyNotes"><%#DataBinder.Eval(Container.DataItem, "ActionListNote")%></span>
                </li>
            </ItemTemplate>
        </asp:Repeater>

5 个答案:

答案 0 :(得分:49)

给出格式,例如:

<%# DataBinder.Eval(Container.DataItem, "ActionListDate", "{0:d/M/yyyy hh:mm:ss tt}") %>

答案 1 :(得分:24)

<%# string.Format("{0:ddd MMM yyyy}", Eval("ActionListDate"))%>

答案 2 :(得分:2)

我把它放在后面的代码中:

public string makeShortDate(object oDate)
{
    if (oDate is DBNull) {
        return "";
    } else {
        DateTime dDate = Convert.ToDateTime(oDate);
        string sDate = dDate.ToShortDateString();
        return sDate;
    }           
}

在XHTML中使用它:

Text='<%# makeShortDate ( DataBinder.Eval(Container.DataItem, "MyDate")) %>'

您可以为任何类型修改此项。

答案 3 :(得分:-1)

您也可以将您的tsql更改为以下内容,或者只删除Convert.ToInt32()

string sql = string.Format(@"select a.ActionListDate, a.LeadListID, 
                            a.ActionListNote, 
                            l.LeadActionName
                            from ActionLists as a
                            INNER JOIN LeadActions as l
                            ON a.LeadActionID = l.LeadActionID
                            where a.LeadListID = {0};", Request["id"]);

答案 4 :(得分:-4)

现在无法测试此代码,但是还有什么内容?

<%#DataBinder.Eval(Container.DataItem, "ActionListDate").ToString("dd/MM/yyyy") %>