title: OData - How to query DateTime greater or equal to given value
body: <p>I am trying to query my OData service for a data with a datetime greater or equal to given value. It is not working as expected</p>
  <p>I have read the documentation:</p>
  <p><a href="https://docs.microsoft.com/en-us/odata/webapi/datetime-support#filter-datetime" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/odata/webapi/datetime-support#filter-datetime</a></p>
  <p><strong>Assemblies</strong>:</p>
  <ul>
  <li>Microsoft.AspNetCore.OData 7.4.0</li>
  <li>NET Core 3</li>
  <li>EF Core</li>
  </ul>
  <p><strong>Reproduce steps</strong></p>
  <p>I have tried the following query formats:</p>
  <ol>
  <li><p><code>https://myurl/api/resource?$filter=city eq 'Kigali' and purchaseDateTime gt 2020-01-01T23:59:59.99Z &amp;$orderby=purchaseDateTime desc</code>  <strong>HTTP 500 - Conversion failed
  when converting date and/or time from character string.</strong></p>
  </li>
  <li><p><code>https://myurl/api/resource?$filter=city eq 'Kigali' and purchaseDateTime gt cast(2020-01-01T23:59:59.99Z,Edm.DateTimeOffset) &amp;$orderby=purchaseDateTime desc</code>  <strong>returns HTTP 500 - Internal
  Server Error</strong></p>
  </li>
  <li><p><code>https://myurl/api/resource?$filter=city eq 'Kigali' and purchaseDateTime gt datetime'2020-01-01T23:59:59.99Z' &amp;$orderby=purchaseDateTime desc</code></p>
  </li>
  </ol>
  <p>On the OData site, this works</p>
  <p><a href="https://services.odata.org/V4/OData/OData.svc/Products?$filter=ReleaseDate%20gt%202002-12-30T23:59:59.99Z" rel="nofollow noreferrer">https://services.odata.org/V4/OData/OData.svc/Products?$filter=ReleaseDate%20gt%202002-12-30T23:59:59.99Z</a></p>
  <p><strong>Expected result</strong></p>
  <p>Return the data according to the query in the same way that the the <a href="http://services.odata.org" rel="nofollow noreferrer">http://services.odata.org</a> responds</p>
  <p><strong>Actual results</strong></p>
  <p>HTTP Bad Request or HTTP 500</p>
  <p><a href="https://docs.microsoft.com/en-us/odata/webapi/datetime-support#filter-datetime" rel="nofollow noreferrer">Microsoft says</a> this should work:</p>
  <pre><code>GET ~/Customers?$filter=Birthday lt cast(2015-04-01T04:11:31%2B08:00,Edm.DateTimeOffset)
  GET ~/Customers?$filter=year(Birthday) eq 2010
  </code></pre>
  <p>Other sources of info I have tried:</p>
  <ol>
  <li><a href="https://stackoverflow.com/questions/11099479/filtering-dates-between-x-y-using-odata">Filtering dates between x &amp; y using ODATA</a></li>
  <li><a href="https://stackoverflow.com/questions/16196273/odata-date-query">ODATA DATE QUERY</a></li>
  <li><a href="https://stackoverflow.com/questions/24489458/odata-query-filter-for-datetime-range/24490137">OData query filter for dateTime range</a></li>
  </ol>