title: ASP.NET Custom Model Binding: DateTime
body: <p><strong><em>The problem</em></strong></p>
  
  <p>At this point I have a problem where my Get action is trying to read a DateTime parameter in a diferent format that is sent.</p>
  
  <p>While the DateTime sent has this format: <code>0:dd/MM/yyyy</code>
  The Get Actions expects: <code>0:MM/dd/yyyy</code></p>
  
  <p><strong><em>The solution (maybe)</em></strong></p>
  
  <p>In order to change what the Get action is expecting I'm using a Custom Model Binding.</p>
  
  <p><strong>The GET Action</strong></p>
  
  <pre><code>    public async Task&lt;IActionResult&gt; Details(int? id, [ModelBinder(typeof(PModelBinder))]DateTime date)
  </code></pre>
  
  <p><strong>The ModelBinder class</strong></p>
  
  <p>Now here are a few things that are missing and I don't know how to complete it properly:</p>
  
  <pre><code>public class PModelBinder : IModelBinder
  {
      public Task BindModelAsync(ModelBindingContext bindingContext)
      {
          string theDate = bindingContext.HttpContext.Request.QueryString["date"]; 
          //What should I write inside the []? 
          //I've tried QueryString["date"] which is the name of the parameter but it says is wrong
          DateTime dt = new DateTime();
          bool success = DateTime.TryParse(date); //Should I apply ParseExact? How should I do it?
          if (success)
          {
              return new //what should I be returning here? dt?
          }
  
      }
  }
  </code></pre>
  
  <p>I've several questions marked as comments in the code above since I'm just starting to understand Custom Model Binding. Hope anyone can give me some advice.</p>
  
  <p>I'm following this article:</p>
  
  <p><a href="https://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization" rel="nofollow noreferrer">https://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization</a></p>
  
  <p>But it's from <strong>2008!!!</strong>, Although it seems valid since it's exactly the problem I'm having with my GET Action (diferent date formats)</p>
  
  <p><strong><em>Update: aditional information</em></strong></p>
  
  <p>The parameter date is defined as:</p>
  
  <pre><code>    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
      public DateTime FechaLInicioLiq { get; set; }
  </code></pre>
  
  <p>and the URL build when calling that GET Action has this structure for the date parameter:</p>
  
  <pre><code>date=10%2F11%2F2017%200%3A00%3A00
  </code></pre>