SP2013 javascript client object model - get server time
-
04-10-2020 - |
Domanda
My client machine and server machine has time offset of 2:30 hours. So when I try to retrieve Date/Time field using JSOM, it gives time according to offset.
oListItem.get_item("Modified");
So how to get server time even when creating new Date()
object. Can it be acheived by new SP.ClientContext.get_current().get_web().get_regionalSettings()
Soluzione
Yes you should get the site's TimeZone
first and then convert your DateTime
accordingly.
function getTimeZoneAndServerDateTime() {
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() {
var info = timeZone.get_information();
var offset = (info.get_bias() + info.get_daylightBias()) / 60.0;
var serverDateTimeNow = new Date(new Date().getTime() - offset * 3600 * 1000).toISOString();
console.log("serverDateTimeNow: " + serverDateTimeNow);
},
Failure
);
}
function Failure() {
alert("error");
}
Also Vadim Gremyachev
has posted nice answer here at sharepoint designer - get current server datetime through javascript REST
Altri suggerimenti
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);
Try this one
var sysDateTime = new Date("<%= System.DateTime.Now %>");
You need to connect your site through COM and then perform this one. May be this can help you