Search code examples
javascriptjqueryajaxodata

How to filter oData call using dynamic date?


I'm making an AJAX call using URL as follows.

http://somecomputer/Service.svc/Method?$filter=SomeDate gt DateTime'2014-08-24'

This works perfectly and and the moment, I generate the string describing the date right before my AJAX call. However, I wonder if it's possible to match the date on dynamic values, such as today or lastyear or what not.

I haven't found any documentation of that when googling so probably it's not possible but I still need to check with the wisdom of SO.


Solution

  • Yes, it is possible. There is such thing as Built-in Query Functions in OData that you can use in query options to represent dynamic values. From the link you can see that there is this function now() under the "Date Functions" category and the ODataLib has already supported it in the URL parser of its latest version. (see the release notes here).

    But there are two things for you to note:

    1. ODataLib only deals with the parsing of such functions in request URLs, the actual logic of it needs to be further implemented by the service implementers.
    2. According to the look up of the OData V3 protocol (section 10.2.3.1.2 on this page), V3 doesn't have such dynamic built-in query functions.