Date and time OData
functions:
day()
month()
year()
hour()
minute()
second()
does not seem to work with SharePoint 2013 REST Service
Error: The query is not valid
But you could consider the following approaches.
Option 1
Utilize SharePoint 2010 REST endpoint:
/_vti_bin/listdata.svc/ListName?$filter=month(<DateColumn>) eq <MonthNumber>
Example:
/_vti_bin/listdata.svc/Employees?$filter=month(Birthday) eq 9
Option 2
- Create a calculated column
BirthdayMonth
and specify the following formula:=MONTH(Birthday)
- Use CAML query to filter list items by month as demonstrated below.
How to retrieve list items using CAML query:
function getListItems(webUrl,listTitle, query)
{
var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
var queryPayload = {
'query' : {
'__metadata': { 'type': 'SP.CamlQuery' },
'ViewXml' : query
}
};
return $.ajax({
url: url,
method: "POST",
data: JSON.stringify(queryPayload),
headers: {
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"Accept": "application/json; odata=verbose",
"content-type": "application/json; odata=verbose"
}
});
}
Usage
var today = new Date();
var monthKey = today.getMonth();
var query = String.format('<View><Query><Where><Eq><FieldRef Name="BirthdayMonth" /><Value Type="Number">{0}</Value></Eq></Where></Query></View>',monthKey);
getListItems(_spPageContextInfo.webAbsoluteUrl,'Empployees',query)
.done(function(data)
{
var items = data.d.results;
for(var i = 0; i < items.length;i++) {
console.log(items[i].Title);
}
})
.fail(
function(error){
console.log(JSON.stringify(error));
});