Wednesday, July 13, 2016

how to take server datetime in Sharepoint 2013 using javascript

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()); // get current SP server time in UTC
})
.fail(
function(error){
    console.log(JSON.stringify(error));
});

No comments:

Post a Comment