Mike C
Mike C

Reputation: 63

C#.net Inline Databinder convert datetime to string with formatting

I have a nested repeater that I want to convert the databound object

<%# DataBinder.Eval(Container.DataItem,"myDate") %>

to a date format of "MM/dd/yy". I have tried

<%# string.Format(DataBinder.Eval(Container.DataItem,"myDate").ToString(),"MM/dd/yyyy") %> in multiple iterations ie..(string)(Databinder.... string.Format((string)(DataBinder.Eval("myDate")),"MM/dd/yy")

as well as <%# DataBinder.Eval(Container.DataItem,"myDate").ToString("MM/dd/yy") %> The last one give me an error that ToString() does not take an argument. I have looked all over and have found nothing that works. Any thoughts?

ACTUAL CODE IN .net page (WebForm): <label class="anj"><%# string.Format(DataBinder.Eval(Container.DataItem,"myDate").ToString(),"MM/dd/yyyy") %> </label>

Upvotes: 0

Views: 676

Answers (1)

Habib
Habib

Reputation: 223187

Use the following:

<%# DataBinder.Eval(Container.DataItem, "myDate", "{0:MM/dd/yyyy") %>

The reason your code with String.Format is failing is because you are trying to apply a date format on a string value, instead of DateTime type value.

Upvotes: 1

Related Questions