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)?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With