-1

I want to get the Max date in the column Date1. This is my code :

function RetrieveLog() {
    var getItemsUrl = "/_api/Web/Lists/GetByTitle('" + ListName +"')/items?$select= ID,Title,Date1,LastDD";

    var promise = $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + getItemsUrl,
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            ArrayRetrieved = data.d.results;
            if (ArrayRetrieved.length > 0) {
                console.log(ArrayRetrieved);
                //LICENSES
                for (var i = 0; i < ArrayRetrieved.length; i++) {
                    var RecentYear = new Date(Math.max.apply(ArrayRetrieved[i].Date1)); // CAN YOU PLEASE CORRECT THIS FUNCTION FOR ME?
                    if (ArrayRetrieved[i].Date1 == RecentYear && ArrayRetrieved[i].Date1 != null && ArrayRetrieved[i].Date1 != "Invalid date") {                                                              
                        $("#LastDD").text(moment(ArrayRetrieved[i].LastDD).format('DD MMM YYYY'));
                    }
                }
            }
        }
    });
}     
  • is date1 column is of type date and time in list? – Ganesh Sanap Feb 25 at 13:21
  • Also, do you want to retrieve all items from list or just single item with the max date in Date1 field? – Ganesh Sanap Feb 25 at 13:27
2

If your Date1 field is of type Date and Time, and you want to retrieve just single item with the maximum(latest) date in Date1 field then try using below URL:

/_api/web/Lists/getbytitle('ListName')/items?$select=Title&$orderby=Date1 desc&$top=1
  • Thank youuu very much – Jihen Feb 25 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.