javascript – Date formatting works in Chrome and not in Internet Explorer, or vice versa, with MVC C#

Question:

If I use in the model

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

It works perfectly in IE but not 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 doesn't load in the edit form when opened.

if i change to

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

There 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 format dd/MM/yyyy ?

if I use

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

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

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

if I use

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

solves the problem in Internet Explorer… but in Chrome it displays dd/mm/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