Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date within View in ASP.NET Core MVC

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.

like image 405
Alvin Quezon Avatar asked May 03 '17 06:05

Alvin Quezon


People also ask

What is FFF in date format?

The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.


1 Answers

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.

like image 196
Francois Borgies Avatar answered Oct 08 '22 08:10

Francois Borgies