Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#Eval Short Date

Tags:

c#

asp.net

eval

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>
like image 826
Mark Fenech Avatar asked Jan 12 '13 13:01

Mark Fenech


4 Answers

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>
like image 87
Daniel Park Avatar answered Oct 08 '22 22:10

Daniel Park


Try this;

<asp:Label ID="Label4" runat="server" Text='<% # Eval("soDateTo", "{0:dd/MM/yyyy}") %>' Font-Bold="False" Font-Size="Small"></asp:Label>
like image 42
mhmoudKotb Avatar answered Oct 08 '22 22:10

mhmoudKotb


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>
like image 5
benscabbia Avatar answered Oct 08 '22 22:10

benscabbia


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>
like image 3
Kirk Avatar answered Oct 08 '22 23:10

Kirk