3

I want to get the timezone in SharePoint, which is set in the regional settings. To be specific it is set as Japan Time. I am using javascript to fetch the time, but unfortunately I can't.

I tried this:

var value = SP.RegionalSettings.get_timeZones ()

which can be found here

and this

var object = new SP.TimeZone()

which can be found here

Is there something that I miss before using this? Or is there something I need to configure before I can use this in my codes.

2 Answers 2

5

With REST:

_api/Web/RegionalSettings/TimeZone

With JSOM:

var ctx = SP.ClientContext.get_current();
var timeZone = ctx.get_web().get_regionalSettings();
ctx.load(timeZone);
ctx.executeQueryAsync(function() {
  console.log(timeZone);
});

2010:
It does not seem easy to do this in 2010. Here is a solution by Marc, which is based of this answer by Waqas Sarwar.
Another options is to use this solution by lem.mallari which use Utility.FormatDateTime to convert a given time to the site timezone. I have converted the last answer from CSOM to JSOM:

var ctx = SP.ClientContext.get_current();
var dt = new Date();
var res = SP.Utilities.Utility.formatDateTime(ctx, ctx.get_web(), dt.toISOString(), SP.Utilities.DateTimeFormat.dateTime);
ctx.executeQueryAsync(function()  {
  console.log(dt, res.get_value());
});
4
  • I have found this same solution, but pardon me though, I am still new to sharepoint and I don't have any clue about REST and JSOM, I'll ask again, is there anything that I need to configure first before putting this in my codes? I tried to run this on my codes and I guess it does not recognize any of the lines. Just a heads up, I am coding in workflow management at sharepoint which is very annoying because it limits the ability to use javascript syntax. Commented May 12, 2015 at 9:12
  • If you are writing a workflow, could you not make it call out to SharePoint? Commented May 12, 2015 at 9:46
  • Anyway to get this to work with SharePoint 2010? Right now I'm getting the error Object doesn't support property or method 'get_regionalSettings'. Commented Dec 7, 2015 at 19:43
  • @IMTheNachoMan Didn't seem like an easy task. I have updated the answer for 2010, at least what I was able to find. Commented Dec 8, 2015 at 6:59
0

JSOM or JavaScript Object Model is a coding practice in which we use JavaScript API reference for SharePoint 2013. Refer this link to get an idea

REST or Representational State Transfer is introduced in SP2013 that is comparable to the existing SharePoint client object models. Refer this msdn link to get more idea and also this MSDN Magazine

To get the time zone below JavaScript function can be helpful

// Global variable to store time zone object.
var siteTimeZone = '';

// function to get time zone
function getTimeZome(){
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var timeZone = web.get_regionalSettings().get_timeZone();
        context.load(timeZone);
        context.executeQueryAsync(
        function onSucceeded() {
            siteTimeZone = timeZone;
        },
         queryFailure
        );

}

// function to handle failuer 
function queryFailure (sender, args) {
    alert('error');
}

//This is the way you call the function, making sure that sp.js is loaded which contains necessary functions.
ExecuteOrDelayUntilScriptLoaded(getTimeZome, "sp.js");

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.