5

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)?

Rohit Waghela
  • 844
  • 3
  • 11
  • 21

2 Answers2

1

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

blayderunner123
  • 306
  • 2
  • 11
  • Can you pls tell, the difference between todays date and another date field.if I wanna compare with a custom_date_field and today's date , whether if its greater than today date or not.. – samolpp2 May 16 '19 at 15:20
0

 var listName = "Carousel%20News";
 var today = new Date();
 //Set the hours for Today
 today.setUTCHours(0,0,0,0); //This will reset today to start from today at 12:00am UTC
 today.toISOString(); //this will return to UTC string

 /*
 today.setHours(0,0,0,0); //This will reset today to local timezone at 12:00 am
 today.toString(); // this will return to YOUR local zimezone string
 */

 var siteURL = _spPageContextInfo.webServerRelativeUrl;
 var url = siteURL + "/_api/web/lists/getbytitle('" + listName + "')/items?$select=Id,Title&$filter=Created eq '" + today + "'"
blayderunner123
  • 306
  • 2
  • 11