23

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.

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

7 に答える 7

58

Try String Formatting within the Eval statement: See ASP Forums

There are several ways to format the date.

<asp:label id="DateAddedLabel" runat="server" text='<%#
Eval("DateAdded", "{0:d}") %>'></asp:label>
于 2013-01-12T14:08:05.727 に答える
17

Try this;

<asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:dd/MM/yyyy}") %>' Font-Bold="False" Font-Size="Small"></asp:Label>
于 2013-01-12T14:08:43.073 に答える
4

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

<asp:label id="DateAddedLabel" runat="server" text=
    '<%# (String.IsNullOrEmpty(Eval("DateAdded").ToString())) 
    ? "No Date Available" : Eval("DateAdded", "{0:d}") %>'>
</asp:label>
于 2016-01-09T21:11:16.283 に答える
3

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.

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

For example if you are using this inside of a 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

<div id="client_since">
    <%# Eval("soDateTo", "{0:MM/dd/yyyy}") %>
</div>
于 2013-11-21T23:34:25.273 に答える
0

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

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

于 2013-01-12T14:06:18.497 に答える
0

Try this:

> <asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:d}") %>'
> Font-Bold="False" Font-Size="Small"></asp:Label>
于 2013-01-12T14:14:17.660 に答える
0

Try this;

<asp:Label ID="lbldate" runat="server" Text='<%# (Convert.ToDateTime(Eval("soDateTo"))).ToShortDateString()  %>'></asp:Label>
于 2017-07-03T09:19:24.253 に答える