WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

How to query DateTime greater or equal to given value

Open tinonetic opened this issue 4 years ago • 3 comments

Short summary (3-5 sentences) describing the issue.

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

I have read the documentation:

https://docs.microsoft.com/en-us/odata/webapi/datetime-support#filter-datetime

*Which assemblies and versions are known to be affected

Microsoft.AspNetCore.OData 7.4.0

Reproduce steps

I have tried the following query formats:

https://myurl/api/resource?$filter=city eq 'Kigali' and purchaseDateTime gt 2020-01-01T23:59:59.99Z &$orderby=purchaseDateTime desc HTTP 500 - Conversion failed when converting date and/or time from character string.

https://myurl/api/resource?$filter=city eq 'Kigali' and purchaseDateTime gt cast(2020-01-01T23:59:59.99Z,Edm.DateTimeOffset) &$orderby=purchaseDateTime desc returns HTTP 500 - Internal Server Error

https://myurl/api/resource?$filter=city eq 'Kigali' and purchaseDateTime gt datetime'2020-01-01T23:59:59.99Z' &$orderby=purchaseDateTime desc

On the OData site, this works

https://services.odata.org/V4/OData/OData.svc/Products?$filter=ReleaseDate%20gt%202002-12-30T23:59:59.99Z

Expected result

Return the data according to the query in the same way that the the http://services.odata.org responds

Actual result

HTTP Bad Request or HTTP 500

Additional detail

Using:

.NET Core 3 EF Core Microsoft.AspNetCore.OData

tinonetic avatar Jul 30 '20 12:07 tinonetic

Are you using DateTimeOffset type for purchaseDateTime, and what is the underlying datatype for this field in the datasource? Can you please share the metadata.

Sreejithpin avatar Aug 04 '20 16:08 Sreejithpin

I had a similar problem, my models contains properties of type DateTime. I solved it by adding data annotation [Column(TypeName = "datetime")] , see #1843.

idrisnacer avatar Jan 21 '21 11:01 idrisnacer

@tinonetic Was this issue resolved? If yes, kindly close it

KenitoInc avatar May 18 '22 08:05 KenitoInc

Closing this issue due to inactivity. If this issue still persists, feel free to create a new issue.

KenitoInc avatar Feb 10 '23 08:02 KenitoInc