I have a property of my model as follows:
[DisplayName("Manufactured Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateManufactured { get; set; }
In my scaffolded MVC Core view I have this code to display that value:
<input asp-for="LiftingItem.DateManufactured" class="form-control" />
In Chrome, the input shows the text "dd/mm/yyyy" and I get the error:
The specified value "09/02/2006" does not conform to the required format, "yyyy-MM-dd".
Whereas in IE I get the actual date (e.g. "09/02/2006"). I think that difference is because Chrome is using the HTML 5 Date type input and so requires the format in "yyyy-MM-dd" format to display it?
If I remove the DisplayFormat
attribute, sure enough it works in Chrome, but then in IE the date is in the wrong format ("yyyy-MM-dd" instead of "dd/mm/yyyy").
So, it seems like using DisplayFormat
with a non "yyyy-MM-dd" format stops the Tag Helpers working? Is there any way to get it working nicely for both HTML 5 compatible and older browsers?
PS: I've also tried applying the formatting in the view instead using asp-format="{0:dd/MM/yyyy}
as described in this question, but that also stops it working in Chrome.
Edit: I could also remove [DataType(DataType.Date)]
, but then I don't get the built in datepicker on HTML5 compatible browsers, which I would definitely like to on mobile devices.