How do I change the date format of a DataBinder.Eval in asp.net?
I put this in the code behind:
public string makeShortDate(object oDate)
{
if (oDate is DBNull) {
return "";
} else {
DateTime dDate = Convert.ToDateTime(oDate);
string sDate = dDate.ToShortDateString();
return sDate;
}
}
And use this in the XHTML:
Text='<%# makeShortDate ( DataBinder.Eval(Container.DataItem, "MyDate")) %>
'
You can modify this for any type.
<%# string.Format("{0:ddd MMM yyyy}", Eval("ActionListDate"))%>
give the format e.g:
<%# DataBinder.Eval(Container.DataItem, "ActionListDate", "{0:d/M/yyyy hh:mm:ss tt}") %>