how to apply particular FORMAT of date by using eval? - asp.net

I have used following code :
<asp:HyperLink ID="Time" runat="server" Text='<%#Eval("CREATED_ON")%>'> </asp:HyperLink>
It will display the date in the format: 11/4/2010 10:52:33 AM
But I want it to display 11/4/2010. How would I do this?

You should be able to use something like this:
<asp:HyperLink ID="lnkCreatedDate" runat="server" Text='<%#Eval("CREATED_ON", "{0:dd/M/yyyy}")%>'> </asp:HyperLink>

You can try this -
<asp:HyperLink ID="lnkCreatedDate1" runat="server" Text='<%# DateTime.Parse(Eval("CREATED_ON").ToString()).ToString("d") %>'> </asp:HyperLink>

this should do the trick:
<asp:HyperLink ID="lnkCreatedDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "CREATED_ON", "{0:dd/MM/yyyy}") %>'></asp:HyperLink>

I suppose you have a DateTime variable named MyDate:
DateTime MyDate;
If you want juste the date part:
MyDate.Value.ToString("d");
With the day:
MyDate.Value.ToString("D");
Here is a usefull doc PDF Doc

I Was Face Problem When Date Is NULL.
It Will Help Me When CREATED_ON Date Is Null.
Use This Code When You Are Not Sure That Your Date Is NULL OR NOT.
<asp:HyperLink ID="lnkCreatedDate1" runat="server" Text='<%# (String.IsNullOrEmpty(Eval("CREATED_ON").ToString())) ? "" : DateTime.Parse(Eval("CREATED_ON").ToString()).ToString("d") %>'></asp:HyperLink>

You Can try THis Also
<asp:HyperLink ID="lnkCreatedDate" runat="server" Text='<%#Eval("CREATED_ON", "{0:d}")%>'> </asp:HyperLink>

Related

How do I format a date pulled from a database?

I am trying to pull a date from database and putting it on a webpage with the below code:
<asp:Label ID="Label3" runat="server" Text='<%# Eval("TravelDate") %>' /><br /><br />
It pulls the date with no problem, but when it shows up on the page, no matter how it is formatted in the database, it seems to want to always display the date as "6/17/2013 12:00:00am". Is there something I'm missing in the VS portion that I have to use to format the date? I'd prefer "June 17, 2013", but the only option close to that in Access is where it adds the day of the week in front of it. Getting rid of the time is important.
<asp:Label ID="txtDate" Width="65px" runat="server" Font-Size="8.5pt" ForeColor="#000f9f"
Text='<%# Eval("How_date","{0:dd/MMM/yyyy}") %>'></asp:Label>
Try to extract like the field like this:
Expr1:Format([Field Name],"DD/MM/YYYY")
or
If it's a text field then you can use the string function left() or right() to get the date. Expr1:Left([Field Name],10)
Just remove the single quotation mark '' after the property text of the TextBox.
<asp:Label ID="Label3" runat="server" Text=<%# Eval("TravelDate", "{0:MMMM dd, yyyy}") %> /><br /><br />

JavaScript dateFormat error?

I am trying to get a field named BASVURUTARIHI(Date) from DataSet. But if I only bind it with Eval, it shows dd/mm/yyyy HH/MM/SS. I don't want the hours and minutes. So, I am trying to change the dateformat but it gives me the error:
CS0103: The name 'dateFormat' does not exist in the current context
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# dateFormat(Eval("BASVURUTARIHI"),"dd/mm/yyyy") %>'></dx:ASPxLabel>
I don't know a lot about JavaScript. Can you help me with this please?
Try this:
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "BASVURUTARIHI", "{0:dd MMM yyyy}")%>'></dx:ASPxLabel>
This is ASP.NET code, try to have this instead:
Text='<%# ((DateTime)Eval("BASVURUTARIHI")).ToString("dd/mm/yyyy") %>'>
Edit: to avoid error when the value is null, change to:
Text='<%# (Eval("BASVURUTARIHI") == null) ? "" : ((DateTime)Eval("BASVURUTARIHI")).ToString("dd/mm/yyyy") %>'>
You can use this code:
<dx:ASPxLabel ID="ASPxLabel2" runat="server" Text='<%# date("j/ n/ Y",Eval("BASVURUTARIHI")) %>'>
</dx:ASPxLabel>

How to set time format on the time of binding?

I want the time format should be like this 11.00 AM. Please tell how can I? Thanks in advance. My code is:
<asp:DataList ID="dlEvents" runat="server" Width="100%">
<ItemTemplate>
<div class="date">
<%#Eval("StartTime")%>
to
<%#Eval("EndTime")%></div>
</ItemTemplate>
</asp:DataList>
Use,
<%#Eval("StartTime","{0:hh.mm tt}")%>
If type field type is string but it contains valid datetime value then use,
<%# DateTime.Parse(Eval("StartTime").ToString()).ToString("hh:mm tt")%>
<%# ((DateTime)Eval("StartTime")).ToString("HH.mm tt")%>
This will help you Custom Date and Time Format Strings
You can find here the format you want in a DATE or TIME..
http://www.geekzilla.co.uk/View00FF7904-B510-468C-A2C8-F859AA20581F.htm
<asp:DataList ID="dlEvents" runat="server" Width="100%">
<ItemTemplate>
StartTime = String.Format("{h:mm tt}", StartTime );
EndTime= String.Format("{h:mm tt}", EndTime);
<div class="date">
<%#Eval("StartTime")%>
to
<%#Eval("EndTime")%></div>
</ItemTemplate>
</asp:DataList>

ASP.NET multiple Eval fields

I need to include multiple Eval fields in the Navigate URL field of a hyperlink control, however, the code I have tried does not work.
<asp:HyperLink ID="hlkImageLink" runat="server" NavigateUrl='<%# Eval("getProductIDGV","getProductCategoryNameGV","getProductCategoryIDGV", "~/PT_productdetails.aspx?ProductID={0}&CategoryName={1}&CategoryID={2}") %>'>
Try:
NavigateUrl= '<%# String.Format("~/PT_productdetails.aspx?ProductID={0}&CategoryName={1}&CategoryID={2}", HttpUtility.UrlEncode(Eval("getProductIDGV")), HttpUtility.UrlEncode(Eval("getProductCategoryNameGV")), HttpUtility.UrlEncode(Eval("getProductCategoryIDGV"))) %>'

Formatting DataBinder.Eval data

How can I format data coming from a DataBinder.Eval statement in an ASPX page?
For example, I want to display the published date of the news items in a particular format in the homepage. I'm using the ASP.NET 2.0 Repeater control to show the list of news items.
The code for this goes like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<HeaderTemplate><table cellpadding="0" cellspacing="0" width="255"></HeaderTemplate>
<ItemTemplate>
<tr><td >
<a href='/content/latestNews.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id") %>'>
<asp:Label ID="lblNewsTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'></asp:Label>
</a>
</td></tr>
<tr><td>
<asp:Label ID="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate"))%>'></asp:Label>
</td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate></asp:Repeater>
Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
If yes, then where do I write the GetDateInHomepageFormat method? I tried out in the code behind page but got a run time error?
If this is not possible, is there a way to do inline formatting?
There is an optional overload for DataBinder.Eval to supply formatting:
<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:
<asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:dddd d MMMM}") %>'</label>
After some searching on the Internet I found that it is in fact very much possible to call a custom method passing the DataBinder.Eval value.
The custom method can be written in the code behind file, but has to be declared public or protected. In my question above, I had mentioned that I tried to write the custom method in the code behind but was getting a run time error. The reason for this was that I had declared the method to be private.
So, in summary the following is a good way to use DataBinder.Eval value to get your desired output:
default.aspx
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
default.aspx.cs code:
public partial class _Default : System.Web.UI.Page
{
protected string GetDateInHomepageFormat(DateTime d)
{
string retValue = "";
// Do all processing required and return value
return retValue;
}
}
Hope this helps others as well.
Why not use the simpler syntax?
<asp:Label id="lblNewsDate" runat="server" Text='<%# Eval("publishedDate", "{0:dddd d MMMM}") %>'</label>
This is the template control "Eval" that takes in the expression and the string format:
protected internal string Eval(
string expression,
string format
)
http://msdn.microsoft.com/en-us/library/3d2sz789.aspx
You can use a function into a repeater like you said, but notice that the DataBinder.Eval returns an object and you have to cast it to a DateTime.
You also can format your field inline:
<%# ((DateTime)DataBinder.Eval(Container.DataItem,"publishedDate")).ToString("yyyy-MMM-dd") %>
If you use ASP.NET 2.0 or newer you can write this as below:
<%# ((DateTime)Eval("publishedDate")).ToString("yyyy-MMM-dd") %>
Another option is to bind the value to label at OnItemDataBound event.
This line solved my problem:
<%#DateTime.Parse(Eval("DDDate").ToString()).ToString("dd-MM-yyyy")%>
To format the date using the local date format use:
<%#((DateTime)Eval("ExpDate")).ToString("d")%>
How to Format an Eval Statement to Display a Date using Date Locale
Thanks to all. I had been stuck on standard format strings for some time. I also used a custom function in VB.
Mark Up:-
<asp:Label ID="Label3" runat="server" text='<%# Formatlabel(DataBinder.Eval(Container.DataItem, "psWages1D")) %>'/>
Code behind:-
Public Function fLabel(ByVal tval) As String
fLabel = tval.ToString("#,##0.00%;(#,##0.00%);Zero")
End Function
Text='<%# DateTime.Parse(Eval("LastLoginDate").ToString()).ToString("MM/dd/yyyy hh:mm tt") %>'
This works for the format as you want
<asp:Label ID="ServiceBeginDate" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:yyyy}") == "0001") ? "" : DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:MM/dd/yyyy}") %>'>
</asp:Label>
You can use it this way in aspx page
<%# DataBinder.Eval(Container.DataItem, "DateColoumnName", "{0:dd-MMM-yyyy}") %>

Resources