How to get current datetime of server

2.5k views Asked by At

Is it possible to get current date time of sharepoint teamsite hosted server through javascript REST api?

1

There are 1 answers

0
Vadim Gremyachev On BEST ANSWER

You could utilize Regional Settings REST endpoint:

/_api/web/RegionalSettings/TimeZone 

to retrieve SharePoint Time Zone settings and then get time for that time zone:

function getSPCurrentTime(webUrl)
{
    return $.ajax({
        url: webUrl + "/_api/web/RegionalSettings/TimeZone",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" }
    }).then(function(data){
         var offset = data.d.Information.Bias / 60.0; 
         return new Date( new Date().getTime() - offset * 3600 * 1000);
    });
}

Usage

getSPCurrentTime(_spPageContextInfo.webAbsoluteUrl)
.done(function(value)
{
    console.log(value.toUTCString());
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});