3

I am creating a custom data entry app in sharepoint using the CSOM. I need to update the default value of a datetime field, I can get the default value but the datetime is in UTC Format.

I need to convert it to whatever format the sharepoint server uses. I have tried using the SP.Web.RegionalSettings.get_Dateformat() method like so:

var context = SP.ClientContext.get_current();
regionalsettings = context.get_web().get_regionalSettings();   //global variable
context.load(regionalsettings);
context.executeQueryAsync(configLoaded, configFailed);
....

function configLoaded(){
var dateformat = regionalsettings.get_dateFormat();

but this just returns a 0 value every time

2 Answers 2

3

You can simply parse the date using Javascript Date object. If different cultures are being used, you have to get the culture being used and format it accordingly.

var date = oListItem.get_item('StartDate');
if(date) {
 date = new Date(date).format('MM/dd/yyyy');
 window.console && console.log(date);
}


//Update
var today = new Date();
oListItem.set_item('StartDate', today);
oListItem.update();
4
  • The problem is I dont know what culture is being used? I'm not updating a list, I'm just putting the default value in a textbox.. So I need to know the dateformat to use (I need to get the culture info from Sharepoint)
    – Michael
    Mar 28, 2014 at 2:29
  • Do you have sites in languages other than English? If so, you have to take into account the culture.
    – Aveenav
    Mar 28, 2014 at 15:59
  • Its all English, but how do I download the culture information from sharepoint?
    – Michael
    Mar 28, 2014 at 22:16
  • There are many ways to get the culture: a. _spPageContextInfo.currentCultureName b. _spPageContextInfo.currentCultureName c. document.getElementsByTagName("html")[0].lang
    – Aveenav
    Mar 29, 2014 at 14:52
3

You could utilize CSOM TimeZone.UTCToLocalTime method to convert the specified date from Coordinated Universal Time (UTC) to local time.

Example

(function(){

  var context = SP.ClientContext.get_current();
  var web = context.get_web();
  var regionalSettings = web.get_regionalSettings();

  var dueDate = new Date();  //set DueDate to [now]
  var localDueDate = regionalSettings.get_timeZone().utcToLocalTime(dueDate);

  context.load(regionalSettings);
  context.executeQueryAsync(
     function() {

         console.log(localDueDate.get_value());
         //
     },
     function(sender,args){
         console.log(args.get_message());
     }
  );

})();

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.