1

I'm working in an MVC application. I would like my date to be always in the format dd/MM/yyyy for example 16/04/2017.

Here's a scenario from my application. I have a SaleOrder.cs entity, I have a public DateTime Date { get; set; } property. When viewing the details of the SaleOrder, I'd like the date to show as 16 Apr 2017 and when editing a SaleOrder, I'd like the input to be populated in the following format 16/04/2017.

This isn't happening for me at the moment. I've captured the current behaviour in the following screencast.

My EditorTemplate for DateTime is like so:

@model DateTime
@{
  IDictionary<string, object> htmlAttributes;
  object objAttributes;
  if (ViewData.TryGetValue("htmlAttributes", out objAttributes))
  {
    htmlAttributes = objAttributes as IDictionary<string, object> ?? HtmlHelper.AnonymousObjectToHtmlAttributes(objAttributes);
  }
  else
  {
    htmlAttributes = new RouteValueDictionary();
  }
  htmlAttributes.Add("type", "date");
  var format = (Request.UserAgent != null && Request.UserAgent.Contains("Chrome")) ? "{0:dd/MM/yyyy}" : "{0:d}";
  @Html.TextBox("", Model, format, htmlAttributes)
}

How can I get my DateTime values to behave?

J86
  • 14,345
  • 47
  • 130
  • 228
  • Are you _really_ sure your `(Request.UserAgent != null && Request.UserAgent.Contains("Chrome"))` does not returns `false`? – Soner Gönül Apr 16 '17 at 15:04
  • Yep, I debugged and that is `true` – J86 Apr 16 '17 at 15:04
  • Seems that `DisplayTemplates` and `EditorTemplates` are suitable way to handle different formatting `DateTime` properties in your issue. Which kind of unobtrusive data validation apply (using `data-val-*` attribute or `jquery.validate`)? – Tetsuya Yamamoto Apr 17 '17 at 05:02
  • You do not need to create an `EditorTemplate` for this. Just use `@Html.TextBoxFor(m => m.Date, "{0:dd/MM/yyyy}")` or if you want the HTML-5 datepicker, then you need to use ISO format - `@Html.TextBoxFor(m => m.Date, "{0:yyyy-MM-dd}", new { type = "date" })` (note you also need to reconfigure the `$.validator` is you using client side validation) –  Apr 17 '17 at 09:14
  • @StephenMuecke reconfigure the validator how? Can you please provide further info? :) – J86 Apr 17 '17 at 09:34
  • I don't think my question is a duplicate, they're using jQuery UI, and I'm not using any such thing. I believe WebKit browsers (e.g. Chrome) give me the picker for free. – J86 Apr 17 '17 at 09:36
  • Depends on if you using the HTML-5 date picker, or a jquery datepicker or a plain textbox for entering a date - refer [here](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969) and [here](http://stackoverflow.com/questions/30594128/error-in-date-validation-mvc/30609111#30609111) –  Apr 17 '17 at 09:38
  • And it is an exact duplicate - that question and the answer relate to the HTML-5 datepicker which you video shows your using (and note that its only supported in Chrome and Edge and it will display a standard textbox in FireFox and IE etc) –  Apr 17 '17 at 09:40
  • my bad @StephenMuecke, I just noticed he began the question with jquery and jquery UI. Does it matter that he's using a `string` for the date? Mine is a `DateTime` proeprty – J86 Apr 17 '17 at 09:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/141866/discussion-between-ciwan-and-stephen-muecke). – J86 Apr 17 '17 at 09:51

0 Answers0