SharePoint Client Object Model expects the dates to be in UTC. Example below show how to update list item dateTime field via JavaScript Client Object Model using the SP.TimeZone.localTimeToUTC method.
function addBrowserUtcOffset(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();
This comment has been removed by the author.
ReplyDeleteI am getting error to this get_regionalSettings()
ReplyDeleteIt says "Object doesn't support this property or method"
This solution not working
ReplyDeleteThanks the solution working fine, just to know instead of date in numbers i tried with the below
ReplyDeletevar theDateTime = new Date(year, month-1, day, hours, minutes).toISOString();
it works fine
so i have modified context with below
this.isoDate = clientContext.get_web().get_regionalSettings().get_timeZone().localTimeToUTC(theDateTime);