title: MVC Date Time Model Binding
body: <p>I am using 2 kendo date pickers in my application as such:</p>
  
  <pre><code>&lt;div class="span12"&gt;
      &lt;div class="span2" style="text-align: right"&gt;
          Start Date:
      &lt;/div&gt;
      &lt;div class="span2"&gt;
          @(Html.Kendo().DatePickerFor(m=&gt;m.StartDate))
      &lt;/div&gt;
      &lt;div class="span2" style="text-align: right"&gt;
          End Date:
      &lt;/div&gt;
      &lt;div class="span2"&gt;
          @(Html.Kendo().DatePickerFor(m=&gt;m.EndDate))
      &lt;/div&gt;
      &lt;div class="span4"&gt;
          &lt;button class="btn btn-primary" onclick="getGraphData()"&gt;Show&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>When the button is clicked, I read the values of these date pickers client side and make a POST to a API controller.</p>
  
  <p>The issue I am having is sometimes the DateTime parameters are parsed incorrectly, I am using a en-GB culture (specified in my web.config), however given a date of 01/03/2014 (1st March), when the value is processed by the model binder, it is interpreted as  03/01/2014 (3rd Jan).</p>
  
  <p>My javascript is as follows:</p>
  
  <pre><code>function getGraphData() {
  
          var startDatePicker = $("#StartDate").data("kendoDatePicker");
          var endDatePicker = $("#EndDate").data("kendoDatePicker");
          var param = {
              StartDate: kendo.toString(startDatePicker.value().toLocaleDateString(), "dd/MM/yyyy"),
              EndDate: kendo.toString(endDatePicker.value().toLocaleDateString(), "dd/MM/yyyy")
          };
         // Do post here
  
      }
  </code></pre>
  
  <p>My model is as follows:</p>
  
  <pre><code>public class DateRangeParam
      {
          #region Constructors and Destructors
  
          /// &lt;summary&gt;
          /// Initializes a new instance of the &lt;see cref="DateRangeParam"/&gt; class.
          /// &lt;/summary&gt;
          public DateRangeParam()
          {
              this.EndDate = DateTime.Today.AddDays(1).AddSeconds(-1);
              this.StartDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
          }
  
          #endregion
  
          #region Public Properties
  
          /// &lt;summary&gt;
          ///     Gets or sets the end date.
          /// &lt;/summary&gt;
          public DateTime EndDate { get; set; }
  
          /// &lt;summary&gt;
          ///     Gets or sets the start date.
          /// &lt;/summary&gt;
          public DateTime StartDate { get; set; }
  
          #endregion
      }
  </code></pre>
  
  <p>I figured the solutions was that I needed a custom model binder to parse the datetime value, so I created on (as follows) and registered it in the Global.asax.cs file, but this didnt work, the binder is never called, I am unsure if this is because the datetime is a property of a custom object.</p>
  
  <pre><code> public class DateTimeModelBinder : IModelBinder
      {
          #region Fields
  
  
          private readonly string _customFormat;
  
          #endregion
  
          #region Constructors and Destructors
  
         public DateTimeModelBinder(string customFormat)
          {
              this._customFormat = customFormat;
          }
  
          #endregion
  
          #region Explicit Interface Methods
  
          object IModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
          {
              ValueProviderResult value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
              return DateTime.ParseExact(value.AttemptedValue, this._customFormat, CultureInfo.InvariantCulture);
          }
  
          #endregion
      }
  </code></pre>
  
  <p>And it is registered as follows:</p>
  
  <pre><code>var binder = new DateTimeModelBinder(new CultureInfo("en-GB").DateTimeFormat.ShortDatePattern);
  ModelBinders.Binders.Add(typeof(DateTime), binder);
  ModelBinders.Binders.Add(typeof(DateTime?), binder);
  </code></pre>
  
  <p>Does anyone know where I am going wrong?</p>