Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm new to filtering dates with REST in SP but am trying to do something like below but am not sure how to get today's date in as a variable into the request. I don't think I'm too far off so any help is greatly appreciated.

    var today = new Date();
    $.ajax({
        url: "/_api/web/lists/GetByTitle('DateItems')/items?$orderby=SortOrder asc&$filter=StartDate ge datetime'" + today + "' and EndDate le datetime'" + today + "'",
        ...
    });
share|improve this question
    
For anyone that stumbles across this, I reversed the 'le and 'ge in the example above. –  Aaron Jul 2 at 23:31

3 Answers 3

up vote 6 down vote accepted

Since your "today" variable is a JavaScript date object, you can get the string representation by calling the toISOString() method.

...$filter=StartDate ge datetime'" + today.toISOString() + "' and ...
share|improve this answer
    
This was the missing piece. Thanks much! –  Aaron Jul 2 at 18:40

I answered the question here (not exactly a duplicate, since that was in regards to the Search REST API, and I happened to give an answer for basic REST).

Here is how you filter dates in REST:

$filter=StartDate ge datetime'2014-01-01T00%3a00%3a00'

This filters for items created after 01-Jan-2014, 00:00:00.

So to filter between two date ranges, simply add another condition to the filter:

$filter=(StartDate ge datetime'2014-01-01T00%3a00%3a00') and (EndDate le datetime'2014-04-30T00%3a00%3a00')

This filters for items created between 01-Jan-2014 and 01-May-2014.

Just create your dates in the format: YYYY-MM-DDTHH:MM:SS, and then encode it (or simply use %3a instead of the colons).

share|improve this answer
    
How can filter just by the Month? –  naijacoder Nov 25 at 6:37
    
@naijacoder How do you mean? You want items where date between March-2014 and July-2014? In that case just use the beginning and end dates of the month (2014-03-01 and 2014-07-31). –  wjervis Nov 25 at 11:51

This article explains REST filtering based on date, http://itblog.wolthaus.net/2011/12/rest-filter-datetime/.

You'd want to pass in a date in YYYY-MM-DD format.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.