【问题标题】:How do I change the date format of a DataBinder.Eval in asp.net?如何在 asp.net 中更改 DataBinder.Eval 的日期格式?
【发布时间】:2011-11-29 02:23:30
【问题描述】:

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

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

我猜它介于 之间,但我不确定。

我的代码是:

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

目前,它看起来像这样:

而我正在寻找的是时间戳消失。

感谢任何帮助。

编辑:

这就是我要找的东西:

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

【问题讨论】:

    标签: c# asp.net sql-server


    【解决方案1】:

    我把这个放在后面的代码里:

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

    并在 XHTML 中使用它:

    Text='&lt;%# makeShortDate ( DataBinder.Eval(Container.DataItem, "MyDate")) %&gt;'

    您可以针对任何类型进行修改。

    【讨论】:

      【解决方案2】:

      您也可以将 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"]);
      

      【讨论】:

      • 你怎么能记下 8 年前的东西!显然方法已经改变#fool
      【解决方案3】:

      &lt;%# string.Format("{0:ddd MMM yyyy}", Eval("ActionListDate"))%&gt;

      【讨论】:

      • 太完美了!
      【解决方案4】:

      给出格式,例如:

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

      【讨论】:

        【解决方案5】:

        目前无法测试此代码,但类似于以下内容?

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

        【讨论】:

          猜你喜欢
          • 2011-11-11
          • 1970-01-01
          • 2018-07-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-13
          • 2019-01-09
          相关资源
          最近更新 更多