Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint REST API filter based on Today's date only and not time. (Similar to IncludeTimeValue=False in CAML query)

I am trying to get data from SharePoint list with REST API that are created today only.

var listName = "Carousel%20News";
var today = new Date().toISOString();

Here is my REST URL :

_spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$select=Id,Title&$filter=Created eq '" + today + "'";

But when I use this rest URL, I am not getting the items that are created today.

(I have double checked that there are items present in the list with the today's created date)

My assumption is that, this URL filters based on Date and also Time value.

So is there way that I can use REST filter with today's date only and ignore the time stamp (like we do IncludeTimeValue=False in CAML query)?

like image 549
Rohit Waghela Avatar asked Nov 08 '22 16:11

Rohit Waghela


1 Answers

I do not know how to get this in CAML. However, I recently just got this to work doing this below. I am using an $.ajax query and a for loop to get my data.

$(document).ready(function()
{
    var siteURL = _spPageContextInfo.webServerRelativeUrl;
    var listName = "Carousel%20News";
	  var url = siteURL + "/_api/web/lists/getbytitle('"+listName+"')/Items";
    $.ajax
    ({
      url: url,
			method: "GET",
			contentType: "application/json; odata=verbose",
			headers: { "Accept": "application/json; odata=verbose" },
			success: function (data)
      {
        var dateTime = new Date();
        var now = Date.parse(dateTime); //convert dateTime to milliseconds
        for (var i = 0; i < data.d.results.length; i++)
        {
          var item = data.d.results[i];
          var ID = item.ID;
          var Created = item.Created;
          /* Lets get the millisecond value for our Date/Time Column */
          var today = Date.parse(Created); //get Millisecond value for Created Date
          var createdDiff = now - today; // get the date difference between now and Created in Milliseconds
          var formatDateDiff = createdDiff/86400000;
          console.log("Item#: "+ID+", "+"Date Difference (24 hr)= "+formatDateDiff);
          
          //Lets only show the results that meet this criteria
          if(formatDateDiff <= 24){
            // do something with your code
          };
        }
      },
      error: function(data)
      {  
        //Give me an error
      }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Let me know if this solved your issue or helps to get you on the right track

like image 82
blayderunner123 Avatar answered Nov 15 '22 04:11

blayderunner123