Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm updating a list with a date field (just the date) using the ECMASCRIPT object model but when the date is inserted is one day off from the introduced date, It's literally making me pull my hair off here's my code:

var fecv = new Date(fecvstring);
fecv = fecv.toISOString();
oListItem.set_item('date', fecv);
oListItem.update();
context.load(oListItem);
context.executeQueryAsync(Function.createDelegate(this, function(){onLotCExt(oListItem);}), Function.createDelegate(this, this.onLotCFail));

Any suggestion?

share|improve this question
    
Dates off by one day - what could possibly be the cause? Time zones perhaps? –  Ryan Aug 21 '12 at 14:28
add comment

2 Answers 2

Example: update ListItem datetime field "dateField" (only for SharePoint 2013)

var addBrowserUtcOffset = function (date) {
    var utcOffsetMinutes = date.getTimezoneOffset();
    var newDate = new Date(date.getTime());
    newDate.setTime(newDate.getTime() - (utcOffsetMinutes*60*1000));

    return newDate;
}


function updateListItem() {
    this.clientContext = new SP.ClientContext();
    var date = new Date(2012, 8, 17, 5, 0);
    console.log(date);
    this.isoDate = clientContext.get_web().get_regionalSettings().get_timeZone().localTimeToUTC(date.toISOString());

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onGetDate),
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onGetDate() {
    var dateToInsert = addBrowserUtcOffset(this.isoDate.get_value());

    var oList = this.clientContext.get_web().get_lists().getByTitle('testlist');
    var oListItem = oList.getItemById(1);
    oListItem.set_item('dateField', dateToInsert.toISOString());
    oListItem.update();

    this.clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onUpdate),
        Function.createDelegate(this, this.onQueryFailed)
    );
}

function onUpdate() {
    console.log('DONE');
}

function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

updateListItem();
share|improve this answer
add comment

SharePoint expects the dates sent back to it via the Client Object Model to be in UTC (aka GMT) time zone.

If you have a date of

2012-08-21 00:00 in New York (which is 4 hours AHEAD of UTC) that =

2012-08-20 18:00 so appears as 'yesterday'

Convert your date/times to UTC before sending to the server.

In sp.js

SP.TimeZone.localTimeToUTC()
share|improve this answer
    
how do I use that method? the documentation says practically nothing, suppose I have a Date object, how do I convert it with that method? –  user1472511 Aug 21 '12 at 14:50
1  
var isoDate = SP.TimeZone.localTimeToUTC(fecv).toString(); oListItem.set_item('date', isoDate); –  Vardhaman Deshpande Aug 21 '12 at 16:15
1  
For those using Moment.js, it is moment(dateValue).utc().toJSON(); –  Robert Kaucher Oct 19 '13 at 2:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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