0

Newb here trying to understand SP and how to manipulate data with REST. I have been able to POST all types of data except dates. When I try this, it doesn't do anything at all for my date column, though it updates my Status column as expected. No console errors and no changes in my date column at all. Any ideas as to what might be going on? I've read every SE question i could find and tried everything suggested but nothing seems to work. Appreciate any advice!

function updateListItem(webUrl,listTitle,listItemId,itemProperties,success,failure)
{
     var listItemUri =  webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/items(" + listItemId + ")";
     var itemPayload = {
       '__metadata': {'type': getItemTypeForListName(listTitle)}
     };
     for(var prop in itemProperties){
           itemPayload[prop] = itemProperties[prop];
     }
     updateJson(listItemUri,itemPayload,success,failure);
}


$("body").on("click",".closeConfirm",function(){
    var newPhase = $(this).parent().siblings(".checkText").find("input").val();
    var newPhaseDate = new Date(newPhase);
    var newPhaseDateJSON = newPhaseDate.toJSON();
    var item = {'CompletionDate': newPhaseDateJSON,'Status': 'Ready to Close'};
    var reqID = $(this).parent().parent().siblings(".requestID").text();
    var reqIDnum = parseInt(reqID);

    var dfd = $.Deferred();
    dfd.done(updateListItem(_spPageContextInfo.webAbsoluteUrl,'MyList',reqIDnum,item,printData,logError));
    dfd.resolve();
    function printData()
    {
        alert("The date has been changed!");
    }
    function logError(error){
        console.log(JSON.stringify(error));
    }
});

Note: I've also tried posting the below variables for 'CompletionDate' (in 'item' var) but no format has done anything.

var newPhase = $(this).parent().siblings(".checkText").find("input").val();
var newPhaseDate = new Date(newPhase);

1 Answer 1

3

in order to post Date to SharePoint list you need to use ISO string 8601 format.

To solve your problem, you can use, the date object and call toISOString() function to get the date in required format.Try the below line of code, this will solve the problem.

var newPhaseDate = (new Date(newPhase)).toISOString();

Also in case if you are filtering against data, you need to use <Col Name> ge/le datetime'<ISO format date>' format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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