Using JQuery to Filter SharePoint List Items by Modified Field

After experiencing a very hard time browsing the internet for answers on how to retrieve items from a SharePoint list using the Modified or Created field as a filter for getting items created or modified on the current day , I then came up with the following implementation in javascript/jquery:

If you are using REST API the datetime literal is used as, e.g. datetime'2013-03-12T12:01:38Z' (This is ISO format) or datetime'2013-03-12′. Therefore, I needed the date in the format yyyy-mm-dd.

Here is the Code for filtering the list:

  1. var d = new Date();   
  2. var date = convertToDate(d);  
  3. //querypath to be used as url when making an ajax call  
  4. var querypath = "http://farmname.com/site/_vti_bin/listdata.svc/listName?$filter=Modified gt datetime'"+date+"'";  
  5. $.ajax({  
  6.  url:querypath,   
  7. type: "GET",  
  8. dataType: "json",  
  9. headers: {  
  10. Accept: "application/json;odata=verbose"  
  11. }  
  12. });  
  13. function convertToDate(date)
  14. {  
  15.    var mnth = date.getMonth() + 1;  
  16.    var day = "";  
  17.    //appending 0 for day less than 10 days since getDate() will for instance return 02 or 03 as just 2 or 3  
  18.   
  19.    if(date.getDate()<10)
  20.    {  
  21.       day = "0"+date.getDate().toString();  
  22.    }  
  23.    else day = date.getDate().toString();  
  24.   
  25.    return date.getFullYear() + "-" + mnth + "-" + day;  
  26. }  
I hope this will help many who face this kind of challenge!

 

Ebook Download
View all
Learn
View all