I'm trying to format a DateTime Variable so that it's being displayed as MMMM dd, yyyy
, e.g. June 10, 2011.
In my view I have:
@Html.Raw(modelItem => item.MyDateTimeProperty.ToString("MMMM dd, yyyy"))
Which throws me an exception as follows:
lambda expression cannot be converted to 'string' because 'string' is not
a delegate type
Of course, the item.MyDateTimeProperty
data type is DateTime
.
For reference I used the following example [1]
// The example displays the following output:
// Today is June 10, 2011.
DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");
What am I doing wrong?
EDIT
I see that stack247
's answer is the most proper solution in an ideal environment. In my case, however, the controller returns an object which doesn't exist in my model classes. This is due I'm consuming an API to get the data. That's why Anrei
's solution is best for me.
@Html.Raw(item.MyDateTimeProperty.ToString("MMMM dd, yyyy"))
[1] http://msdn.microsoft.com/en-US/library/8kb3ddd4%28v=vs.110%29.aspx