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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With