Search code examples
asp.net-mvc-3razor

Converting DateTime format using razor


What is wrong with the following?

@Convert.ToDateTime((@item.Date.ToShortDateString())," dd - M - yy")

@item.Date is showing 20/11/2005 12:00 a.m and I want to display 20 Nov 2011


Solution

  • Try:

    @item.Date.ToString("dd MMM yyyy")
    

    or you could use the [DisplayFormat] attribute on your view model:

    [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
    public DateTime Date { get; set }
    

    and in your view simply:

    @Html.DisplayFor(x => x.Date)