Home > other >  How to show only date in index view in ASP.NET MVC?
How to show only date in index view in ASP.NET MVC?

Time:11-17

In Model:

public DateTime? FromDate { get;set; }

In view:

<div >
    <label >
        From Date
    </label>
    <div >
        @Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control", type = "Date", min = "1944-01-01", max = "2019-01-01" } })
        @Html.ValidationMessageFor(model => model.FromDate, "", new { @class = "text-danger" })
    </div>
</div>

When we enter the date from the DateTime picker calendar it enter the date:

When we enter the date from the DateTime picker calendar it enter the date

In Index view it shows the time also:

In Index view it shows the time also

problem is that I do not want to show the time with the date in the Index view .

CodePudding user response:

To set display format for the DateTime property type use the DisplayFormat attribute in the model declaration, like below:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime? FromDate { get; set; }    
  • Related