Display only Date in @Html.DisplayFor() in MVC

asp.net-mvc-5 entity-framework

Question

I work in MVC5 and EntityFramework for displaying data into View.

right now in my view data format is :- 10/22/2014 12:00:00 AM But i want only date :- 22/10/2014 in this format

i try this code but it not working

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

I also follow few link in stackoverflow but not helpful Date Format using Html.DisplayFor() in MVC5, Display only date and no time

1
5
5/23/2017 10:31:20 AM

Accepted Answer

I didn’t use @html.DisplayFor().Instead of this I use

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

And it works perfectly.

23
5/19/2019 6:17:22 PM

Popular Answer

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..



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow