1

I have a column [ScheduleTime] which is a DateTime type in my List. Using JavaScript i need to update the [ScheduleTime] column which should be Current Day whenever my script loads. But here I need to update [ScheduleTime] column's Date only. Time should remain same. I am gonna use set_item function.

Please help me out on this.

var collListItem;


ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

 function retrieveListItems() {


sucess();


var clientContext = new SP.ClientContext(siteUrl);

var oList = clientContext.get_web().get_lists().getByTitle('Master Task Summary');





var camlQuery = new SP.CamlQuery();

camlQuery.set_viewXml("<View><Query><Where><Geq><FieldRef Name='ScheduleTime'/><Value IncludeTimeValue='true' Type='DateTime'><Today /></Value></Geq></Where></Query><RowLimit>10</RowLimit></View>");


this.collListItem = oList.getItems(camlQuery);

clientContext.load(collListItem);

clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));        

}

function sucess()
{ 

   var clientContext1 = new SP.ClientContext(siteUrl);

    var oList1 = clientContext1.get_web().get_lists().getByTitle('Master Task Summary');

var varlist= oList1.getItemById(86);

alert("1st");

   var scheduleTime = new Date(varlist["ScheduleTime"]);

alert("2st");

var today = new Date();

var newScheduledTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), scheduleTime.getHours(), scheduleTime.getMinutes(), scheduleTime.getSeconds(), scheduleTime.getMilliseconds());

 alert("3st");
 varlist.set_item('ScheduleTime', newScheduledTime);
alert("4th");
 varlist.update();
alert("5th");

  clientContext1.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceed), Function.createDelegate(this, this.onQueryFailed)); 

  }

function onQuerySucceed()
{
alert("Update USccess");
}

1 Answer 1

4

You can make use of JavaScript Date Object for that.

For example, while reading the data

var scheduledTime = new Date(oListItem["ScheduledTime"]);

Now you can calculate the new Time as follows

var today = new Date();
var newScheduledTime = new Date(today.getYear(), today.getMonth(), today.getDate(), scheduledTime.getHours(), scheduledTime.getMinutes(), scheduledTime.getSeconds(), scheduledTime.getMilliseconds());

Now while saving use newScheduledTime

oListItem.set_item('ScheduledTime', newScheduledTime);
oListItem.update();
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

Update based on edit

ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");
var item;
var clientContext;
var oList;
function retrieveListItems() {
    clientContext = new SP.ClientContext(siteUrl);
    oList = clientContext.get_web().get_lists().getByTitle('Master Task Summary');
    item= oList.getItemById(86);
    clientContext.Load(item,"Include('ScheduleTime')");
    clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
}

function onQuerySucceed() { 
    alert("Item Retrieved"); 
    var scheduleTime = new Date(item.get_item('ScheduleTime'));
    alert(scheduleTime);
    var today = new Date();
    var newScheduledTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), scheduleTime.getHours(), scheduleTime.getMinutes(), scheduleTime.getSeconds(), scheduleTime.getMilliseconds());
    alert(newScheduledTime);
    item.set_item('ScheduleTime', newScheduledTime);
    item.update();
    clientContext.executeQueryAsync(Function.createDelegate(this, onUpdateSucceeded), Function.createDelegate(this, onQueryFailed));
}

function onUpdateSucceeded() {
    alert('Updated');
}

function onQueryFailed(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
}
8
  • Thanks for your quick response . var scheduledTime = new Date(oListItem["ScheduledTime"]); Is this mandatory to include in code?
    – Mathikumar
    Commented Jan 28, 2015 at 22:36
  • And also in the third statement var newScheduleTime =new Date(today.getDate()); is enough rgt? Because i need to update only current day not Time.Is that fine?
    – Mathikumar
    Commented Jan 28, 2015 at 22:39
  • I am trying this. Thanks. I will update once it is done
    – Mathikumar
    Commented Jan 28, 2015 at 22:41
  • Request failed. The string was not recognized as a valid datetime. There is a unknown word starting index 1.Undefined.
    – Mathikumar
    Commented Jan 28, 2015 at 23:03
  • You can add couple of alert statements and debug Commented Jan 28, 2015 at 23:08

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.