Show only date in @ Html.DisplayFor () in MVC

I am working in MVC5 and EntityFramework to display data in a View.

right now, in my opinion, the data format is: - 10/22/2014 12:00:00 But I only want the date: - 10/22/2014 in this format

I try this code but it doesn't work

 @if (item.chekin_on_after != null)
                    { @Html.DisplayFor(modelItem => item.chekin_on_after, "ShortDateTime")}

      

I also follow a few links on stackoverflow but not helpful Date format using Html.DisplayFor () in MVC5 , Only display date and time no

+4


source to share


3 answers


I have not used I use @html.DisplayFor()

instead

@Convert.ToString(string.Format("{0:dd/MM/yyyy}", item.chekin_on_after))

      



And it works great.

+17


source


Try to use below attribute in your model class.

[DisplayFormat(ApplyFormatInEditMode = true,DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime chekin_on_after { get; set; }

      



Hope it helps.

+3


source


You can use this function in your controller before filling in data for the model:

public static System.DateTime ParseDate(string value, string style)
{
    DateTime blankDate = default(DateTime);
    if ((value != null & value.Length > 0)) {
        string formattedDate = Strings.Format(Convert.ToDateTime(value), style);
        return formattedDate;
    } else {
        return blankDate;
    }
}

      

Where the meaning is yours: 10/22/2014 12:00:00 AM

and the style: "MM/dd/yyyy"

or whatever you want

+1


source







All Articles