Is it possible to get current date time of SharePoint Team Site hosted server through JavaScript REST API?
4 Answers
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()); // get current SP server time in UTC
})
.fail(
function(error){
console.log(JSON.stringify(error));
});
-
This will produce the wrong value during DST if the site uses a time zone with DST.JLRishe– JLRishe2017-11-10 10:17:07 +00:00Commented Nov 10, 2017 at 10:17
The accepted answer here is wrong because it does not account for DST (daylight savings time).
Using _spPageContextInfo also doesn't always work because that's not necessarily populated (especially if you're making a provider-hosted app).
To get the current time in the timezone of a particular site, you can use the /_api/web/RegionalSettings/TimeZone/utcToLocalTime endpoint, passing it the current UTC time. You can also use this to convert any time to the server's timezone:
function parseDate(isoString) {
var parts = /(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/.exec(isoString);
return new Date(parts[1], parts[2] - 1, parts[3], parts[4], parts[5], parts[6]);
}
function convertToSiteTime(webUrl, dateTime)
{
return $.ajax({
url: webUrl +
"/_api/web/RegionalSettings/TimeZone/utcToLocalTime(@date)?@date='"+
dateTime.toUTCString() +
"'",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" }
}).then(function (data){
return parseDate(data.d.UTCToLocalTime);
});
}
Example usage:
convertToSiteTime('https://sharepoint.mydomain.com/my/site/', new Date())
.then(function (result) { console.log(result); })
You can also expand upon the above to determine the time offset between the user's local time and the site's local time, and then use that to calculate dates thereafter. Note that this would not be a good idea for calculating dates that are far apart because the offset could be different at different parts of the year depending on DST:
function getTimeOffset() {
var referenceTime = new Date();
return convertToSiteTime('https://qdabra.sharepoint.com', referenceTime)
.then(function(result) {
return (referenceTime - result) / 1000 / 60;
});
}
getTimeOffset().then(function (offset) {
console.log('The local time is', offset, 'minutes ahead of the site\'s time.');
});
-
Interesting. I saw the Timezone endpoint in the documentation, but how did you find out about the utcToLocal method? I need to be able to find the next 5pm based on the server time and subtract the difference between the current time. I think two calls here would work for me. Thank you!Dinerdo– Dinerdo2018-04-14 19:52:31 +00:00Commented Apr 14, 2018 at 19:52
-
@Dinerdo I can't remember how I found it, but I think I originally learned there was such a feature in CSOM by looking for ways to convert times into site times and then worked my way out from there. FYI I've updated my answer to show an example of how you can get the time offset between the user's timezone and the site's timezone, which means you could probably pull off what you're trying to do with one call instead of two.JLRishe– JLRishe2018-04-15 14:20:44 +00:00Commented Apr 15, 2018 at 14:20
Vadim's answer does not get the server time. It will return the time set on the local computer (OS Time). You can verify this very easily by changing the date on your computer and see the time that comes up.
The data from that endpoint only includes time zone information.
Looking for an easier solution. Hopefully we don't have to include sp.js to use _spPageContextInfo just to get the time :-/
I am calling from a blank - non templated page.
Once you have access to _spPageContextInfo, you can get the server time from:
_spPageContextInfo.serverTime
It will give you something like this:
2017-07-18T20:49:59.0827749Z
See how to get the _spPageContextInfo on a blank ASPX page from:
Bare minimum page to get _spPageContextInfo loaded
Cheers!
I tried the accepted answer but it seems to give wrong time. I found this post which explains a better way and it seems to work for me always.
You use _spPageContextInfo.clientServerTimeDelta to get the time delta and then add that to current date time.
var currentServerDateTime = new Date(new Date().getTime() + _spPageContextInfo.clientServerTimeDelta);
-
1Please don't add the exact same answer to this question, as you did on this one. Instead, try to specify your answer further to make it a better of this specific question. Thank You!2017-01-03 07:36:40 +00:00Commented Jan 3, 2017 at 7:36