javascript – Date formatting works in Chrome and doesn't work in Internet Explorer, or vice versa with MVC C#

Question:

If I use it in the model

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

It works perfectly in IE but it doesn't work in Chrome . When I open the edit form, instead of showing the date, it shows dd/mm/yyyy . I can edit and the change is saved. But the value does not load into the edit form when opened.

if i change to

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime data { get; set; }

Then it works perfectly in Google Chrome , but in IE . the date appears as 2015-08-10 , for example…

How can I make it work equally in both browsers, with the dd/MM/yyyy format?

if i use

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

generates the error: System.FormatException: The input string was not in an incorrect format.

If I use Html.TextBoxFor instead of Html.EditorFor it works both… the problem is that it has time, in the format. E.g.: "06/15/2015 00:00:00"

if i use

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]

resolves issue in Internet Explorer… but in Chrome it displays mm/dd/yyyy

I don't know what else to do… does anyone have an efficient solution?

Thanks!

Answer:

Try:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
Scroll to Top