$filter option for DateTime in WCF Data Services query URL
Asked Answered
D

2

5

WCF Data Services conveniently allow querying and filtering data via options in the URL. For example, say I want Products with a price greater than 20:

http://www.example.com/Service.svc/Products?$filter=Price gt 20

But how do I use the $filter option with a DateTime? Say I want all Products that have been modified this month.

http://www.example.com/Service.svc/Products?$filter=ModifiedDate gt '2012-05-02'

This doesn't work for me; it gives the error message

Operator 'gt' incompatible with operand types 'System.DateTime' and 'System.String' at position 13.

I get the same with other comparison operators (ge, lt, le, eq). What is going on? How do I make this work? Do I need a certain DateTime format? What I tried above seems to be documented here.

Duggins answered 30/5, 2012 at 22:32 Comment(0)
A
9

Hi try using the following syntax

$filter=ModifiedDate gt datetime'2012-05-02T00:00:00'

The other date time functions can be found in this reference for odata url conventions

URI conventions :

http://msdn.microsoft.com/en-us/library/dd728283.aspx

Alchemist answered 30/5, 2012 at 22:54 Comment(0)
R
3

For DateTime filtering in oData feeds qualify the datetime variable with a DateTime prefix.

   http://odata.netflix.com/v2/Catalog/Titles?$filter=DateModified eq DateTime'2012-01-31T09:45:16'

The above url shows how to filter the netflix odata feed based on date time.

Rifle answered 30/5, 2012 at 22:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.