C# #Eval 短日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14294022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:19:25  来源:igfitidea点击:

#Eval Short Date

c#asp.neteval

提问by Mark Fenech

I am trying to add date From and date To to my products these values are store in my database as date. These are stored in this format 2013-01-15. The format is not a problem but when I display them on my application the time appears (1/15/2013 12:00:00 AM) how can I remove the time please. Below you can find the method Im databound the data.

我正在尝试将日期 From 和 date To 添加到我的产品中,这些值作为日期存储在我的数据库中。它们以这种格式存储2013-01-15。格式不是问题,但是当我在我的应用程序上显示它们时,时间出现 ( 1/15/2013 12:00:00 AM) 我该如何删除时间。您可以在下面找到方法 Im 数据绑定数据。

<asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo") %>' Font-Bold="False" Font-Size="Small"></asp:Label>

采纳答案by Daniel Park

Try String Formatting within the Eval statement: See ASP Forums

在 Eval 语句中尝试字符串格式: 请参阅 ASP 论坛

There are several ways to format the date.

有几种方法可以格式化日期。

<asp:label id="DateAddedLabel" runat="server" text='<%#
Eval("DateAdded", "{0:d}") %>'></asp:label>

回答by IrishChieftain

Use DateTime.ToShortDateStringMethod to get rid of the time part of the date:

使用DateTime.ToShortDateStringMethod 去掉日期的时间部分:

http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

回答by mhmoudKotb

Try this;

尝试这个;

<asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:dd/MM/yyyy}") %>' Font-Bold="False" Font-Size="Small"></asp:Label>

回答by vbuser2004

Try this:

尝试这个:

> <asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:d}") %>'
> Font-Bold="False" Font-Size="Small"></asp:Label>

回答by Kirk

This has been answered just fine, but I used to use a lot more Labels than were necessary and thought I'd offer a way without.

这已经得到了很好的回答,但是我曾经使用了Label比必要更多的s 并且认为我会提供一种没有的方法。

You can ignore the Labelall together and put the Eval(...)method by itself.

您可以忽略Label所有这些并单独放置Eval(...)方法。

For example if you are using this inside of a TemplateField

例如,如果您在一个 TemplateField

<asp:TemplateField HeaderText="Date To">
    <ItemTemplate>
        <%# Eval("soDateTo", "{0:MM/dd/yyyy}") %>
    </ItemTemplate>
</asp:TemplateField>

You can use this to improve your CSS control a tad, such as

您可以使用它来稍微改进您的 CSS 控件,例如

<div id="client_since">
    <%# Eval("soDateTo", "{0:MM/dd/yyyy}") %>
</div>

回答by benscabbia

Very similar to Daniel's solution, but it handles null:

与 Daniel 的解决方案非常相似,但它处理 null:

<asp:label id="DateAddedLabel" runat="server" text=
    '<%# (String.IsNullOrEmpty(Eval("DateAdded").ToString())) 
    ? "No Date Available" : Eval("DateAdded", "{0:d}") %>'>
</asp:label>

回答by Enes Taha Selek

Try this;

尝试这个;

<asp:Label ID="lbldate" runat="server" Text='<%# (Convert.ToDateTime(Eval("soDateTo"))).ToShortDateString()  %>'></asp:Label>