SharePoint REST API过滤器仅基于今天的日期而非时间。 (类似于CAML查询中的IncludeTimeValue = False)

时间:2017-09-14 11:59:53

标签: rest sharepoint-2013

我正在尝试使用今天创建的REST API从SharePoint列表中获取数据。

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

这是我的REST网址:

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

但是当我使用此休息网址时,我无法获取今天创建的项目。

(我已仔细检查过列表中是否存在今日创建日期的项目)

我的假设是,此网址会根据日期和时间价值进行过滤。

那么我可以使用REST过滤器只使用今天的日期并忽略时间戳(就像我们在CAML查询中IncludeTimeValue=False一样)吗?

2 个答案:

答案 0 :(得分:1)

我不知道如何在CAML中获得这个。但是,我最近刚刚开始这项工作。我正在使用$ .ajax查询和for循环来获取我的数据。

$(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>

如果这解决了您的问题或帮助您走上正确的轨道,请告诉我

答案 1 :(得分: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 + "'"