title: Pass datetime parameter in Web API?
body: <p>I want to ask how to pass datetime parameter in Web API, how to pass datetime parameter in optional date. I want to search in the URL can be optional date without time such as:</p>
  <pre><code>localhost:IP/api/values?date=2020-01-01
  </code></pre>
  <p>Expected results:</p>
  <pre><code>&lt;ArrayOfTest xmlns:i=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  &lt;Test&gt;
  &lt;UserId&gt;341&lt;/UserId&gt;
  &lt;Name&gt;Emily&lt;/Name&gt;
  &lt;Mobile&gt;386754298&lt;/Mobile&gt;
  &lt;Age&gt;24&lt;/Age&gt;
  &lt;Date&gt;2021-11-06T16:04:00&lt;/Date&gt;
  &lt;/Test&gt;
  &lt;Test&gt;
  &lt;UserId&gt;2555&lt;/UserId&gt;
  &lt;Name&gt;Peter&lt;/Name&gt;
  &lt;Mobile&gt;48295729&lt;/Mobile&gt;
  &lt;Age&gt;45&lt;/Age&gt;
  &lt;Date&gt;2020-10-12T20:35:00&lt;/Date&gt;
  &lt;/Test&gt;
  </code></pre>
  <p>It can found out date after 2020-01-01. some value are null from SQL Server database, so I used <code>dbnull</code> to make sure the code works and I didn't use Entity Framework to connect the database, I have try to just pass datetime parameter in get method and it is not work as well. Is it possible to pass datatime parameter like this way?</p>
  <p>Class code:</p>
  <pre><code>    public class TestClass
      {
  
          public string UserId { get; set; }
  
          public string Name { get; set; }
  
          public string Mobile { get; set; }
  
          public int Age { get; set; }
  
          public DateTime? Date { get; set; }
      }
  </code></pre>
  <p>controller code:</p>
  <pre><code>        public IHttpActionResult Get(DateTime date)
          {
              List&lt;UserClass&gt; Test = new List&lt;UserClass&gt;();
              string mainconn = ConfigurationManager.ConnectionStrings[&quot;myconn&quot;].ConnectionString;
              SqlConnection sqlconn = new SqlConnection(mainconn);
              string sqlquery = &quot;SELECT UserID, Name, Mobile, Age, Date From tbluser where Date=&quot;+date;
              sqlconn.Open();
              SqlCommand sqlcomm = new SqlCommand(sqlquery, sqlconn);
              SqlDataReader reader = sqlcomm.ExecuteReader();
              while (reader.Read())
                  {
                      Test.Add(new UserClass()
                      {
                          UserId = reader.GetValue.ToString(0),
                          Name = reader.GetValue.ToString(1),
                          Mobile = reader.GetValue.ToString(2),
                          Access = Convert.ToInt32(reader.GetValue(3)),
                          Date = Convert.ToDateTime(reader.GetValue(4))
                      });
                  }
              return Ok(Test);
          }
  </code></pre>