title: Date Time validation in asp.net aplication
body: <p>So I have this asp.net application built using razor and C# and I can't get the validation for date time to work correctly.<br>
  Here are the relevant sections of my app.</p>
  
  <pre><code>public class EmployeeDto
      {
      ...
      [Required]
      [DataMember]
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] // from what I understand this should format the date on the view ackording to the string
      public Nullable&lt;DateTime&gt; inDate { get; set; }
  
      ...
  
      [Required]
      [DataMember]
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
      public Nullable&lt;DateTime&gt; birthDate { get; set; }
  
      [DataMember]
      [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
      public Nullable&lt;DateTime&gt; createdDate { get; set; }
      ...
     }
  </code></pre>
  
  <p>This DTO is also used as a view model.</p>
  
  <p>On the add employee view, we use a date-picker to edit the first two dates. The third is hidden.</p>
  
  <p>Here's what the view looks like</p>
  
  <pre><code> @model StratecEMS.Application.DTO.EmployeeDto
   &lt;style type="text/css"&gt;...&lt;/style&gt;
   &lt;script type="text/javascript"&gt;
  $(function () {
      $("#birthDate").datepicker({ dateFormat: 'dd/mm/yy' });
      $("#inDate").datepicker({ dateFormat: 'dd/mm/yy' });
  });
  ...
  &lt;/script&gt;
  ...
  &lt;fieldset&gt;
      &lt;legend&gt;New Employee Details&lt;/legend&gt;
      @using (Html.BeginForm("AddEmployee", "Administration", FormMethod.Post, new { id = "AddEmployeeForm" }))
      {
          @Html.HiddenFor(model =&gt; Model.createdDate)
          &lt;div class="editor-label"&gt;
              @Html.Label("In Date:")
              @Html.EditorFor(model =&gt; model.inDate)
              @Html.ValidationMessageFor(model =&gt; model.inDate)
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
              @Html.Label("Birthdate:")
              @Html.EditorFor(model =&gt; model.birthDate)
              @Html.ValidationMessageFor(model =&gt; model.birthDate)
          &lt;/div&gt;
       }
    &lt;/fieldset&gt;
  </code></pre>
  
  <p>Now here's my problem:
  On my PC I have the date in international format "yyyy-MM-dd".
  On the application we need the date to always be in a custom format "dd/MM/yyyy" however the validation is always done in the US format "MM/dd/yyyy" and I'm at a loss why this happens.</p>
  
  <p>To make things even stranger after adding the DisplayFormat attribute to the DTO, the two dates that are displayed on my UI are shown in this format "dd-MM-yyyy". WTF!?! but the validation is sill done in the US format.</p>
  
  <p>I can get around the validation for the birthDate and inDate by entering the dates in the US format in the text-boxes, however there is no such workaround for createdDate which is always hidden and remains in the in the international format.</p>
  
  <p>I've also tried to change the culture of the application by setting the thread culture in the Global.asax Application_Start method using this piece of code</p>
  
  <pre><code>var culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
  culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
  Thread.CurrentThread.CurrentCulture = culture;
  Thread.CurrentThread.CurrentUICulture = culture; 
  </code></pre>
  
  <p>However this too seems not to work.</p>
  
  <p>If you had the patience to read this until the end. Do you happen to know a good solution for this predicament?</p>