Format date within View in ASP.NET Core MVC

asp.net-core, asp.net-core-mvc, c#

Solution

Thank you @Enrico for your comment i add it in the answer :

Try it with this in your model:

[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]

And In your controller change DateTime to Date :

myList.loanContract = new LoanContract { LoanDateStart = Date.Today };

Hope this help you.

Problem

I have problem in regarding with converting the datetime to date using a model. Model from Class Library ``` public partial class LoanContract { [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")] public DateTime LoanDateStart { get; set; } } ``` Model from Project ``` public class ModelLoan { public LoanContract loanContract { get; set; } } ``` Code in controller ``` myList.loanContract = new LoanContract { LoanDateStart = DateTime.Today }; ``` View: ``` <input disabled type="date" asp-for="loanContract.LoanDateStart" id="dpDateNow" class="form-control" /> ``` It show like this: `yyyy-MM-dd` what I want to achieve is that I want to change it to `MM/dd/yyyy`. I tried using `.ToString("MM/dd/yyyy")` but it doesn't work.

Original source

Related problems