0

I keep having this issue trying to get the right dateformat.

Image of the EditorFor: EditorFor

I would like for it to be in the (dd/MM/yyyy) format instead.

Here is my Razor viewcode:

<div class="form-group" id="divtwo">
    @Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.EndDate, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control" ,@id = "endDate" } })
        @Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
    </div>
</div>

Model

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

I have also tried setting Globalization tag in web.config, didn't help either. Can you guys give me a hint in the right direction?

Draken
  • 3,134
  • 13
  • 34
  • 54
  • It needs to be `DataFormatString = "{0:yyyy-MM-dd}"` (ISO format). And its just `@Html.EditorFor(m => m.EndDate, new { htmlAttributes = new { @class = "form-control" } })` –  Apr 19 '17 at 08:21
  • Hey, changed [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] in the model, and removed the "{0:dd/MM/yyyy}" from the razorview. Didn't fix the problem – user3759748 Apr 19 '17 at 08:27
  • @Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" ,@id = "endDate" } }) – user3759748 Apr 19 '17 at 08:28
  • Yes it does! (and adding `@id = "endDate"` is pointless (the method already generates `id="EndDate"`). It will display the date in the browsers culture - which is what the HTML-5 datepicker it is designed to do –  Apr 19 '17 at 08:29
  • hmm ok, then maybe my browsers culture may provide the issue. is there a way to force a culture? and thanks for the tip with @id changed it now. – user3759748 Apr 19 '17 at 08:36
  • Only if your users change their culture on their device. And considering that the HTML-5 datepicker is only supported in Chrome and Edge, I would recommen you use a jquery datepicker which gives you far more flexibility. –  Apr 19 '17 at 08:39
  • yes, i agree Jquery datepicker is alot smoother. I will try out with the Datepicker instead. Thanks for helping me out. – user3759748 Apr 19 '17 at 08:44
  • Just also note [this answer](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969) if you want client side validation –  Apr 19 '17 at 08:45
  • Thanks for all your help, it was my server that was set to a wrong lang as soon as i tested on my pc it worked. and i do want client side validation. – user3759748 Apr 19 '17 at 09:31

0 Answers0