0
votes

I am trying to fetch just the date from the DateTime Entity in the View which I created in the Models. But instead it shows the whole DateTime Format

I added an Annotation [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")], but the issue remains.

//Models: 
 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
        public DateTime? BirthDate { get; set; }


//Adding Migration, it processed it as: 
BirthDate = table.Column<DateTime>(nullable: false)


//Index View: 
@foreach(var customers in Model)
{
 @customers.BrithDate
}

I expect the output as Friday January 1, 1990 or 1/1/1990 at least. But the output is 1/1/1990 12:00:00 AM

2

2 Answers

0
votes

The problem is you are outputting the model value directly. If you want to use the display formatting, you should use the following helper instead.

@Html.DisplayFor(m => customers.BirthDate)

Here are some more details in this other question as to why.

When should I use Html.Displayfor in MVC

0
votes

You have to say which DataType you want to use. I think this should solve your problem:

 [DataType(DataType.Date)]
 [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
 public DateTime? BirthDate { get; set; }

Happy coding! :)