0

ASP.NET MVC 5 Date won't be shown in Chrome.

There is no any culture settings in app.

How to fix it?

Html

 @Html.EditorFor(model => model.Changed,
                           new
                           { htmlAttributes =
                                    new { @class = "form-control", @Value = Model.Changed.ToString("MM/dd/yyyy") } })

Model

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime Changed { get; set; }

enter image description here

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Possible duplicate of [MVC4 DataType.Date EditorFor won't display date value in Chrome, fine in Internet Explorer](http://stackoverflow.com/questions/12633471/mvc4-datatype-date-editorfor-wont-display-date-value-in-chrome-fine-in-interne) – J. Swietek Sep 14 '16 at 13:39
  • @J.Swietek No no no It does not work, man – NoWar Sep 14 '16 at 13:40
  • Did you try to remove the DataType attribute as said in the linked answer? – J. Swietek Sep 14 '16 at 13:41
  • @J.Swietek If I remove it I lost the Chrome CALENDAR, But I need it. – NoWar Sep 14 '16 at 13:44
  • What's this 'aaaa' in your editor? Did you change the output manually ? What's the real output? Try with [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}" ...] – greenhoorn Sep 14 '16 at 14:01

1 Answers1

3

In browsers that support the HTML5 date, time, and datetime input types, the value supplied must be in ISO format, i.e. YYYY-MM-DD for date, YYYY-MM-DDTHH:MM:SS for datetime, etc. If it's not in that format, the browser cannot parse the value and therefore treats the input as if it has no value. So all you need is to change your DisplayFormat attribute to:

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Note also OP needs to remove `new { @Value = Model.Changed.ToString("MM/dd/yyyy") }` –  Sep 14 '16 at 23:07