4

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()

3 Answers 3

6

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

6
  • 1
    Thanks. That works fine for retrieving Server Date. But how to save the date/time in server date/time format ?
    – Yayati
    Commented Jul 17, 2015 at 9:31
  • Where do you want to save date values? In some list column? Also keep in mind that sharepoint stores date in utc format.
    – Asad Refai
    Commented Jul 17, 2015 at 9:45
  • Yes.There is an editable Date/Time Column which I have to set with user editable date.
    – Yayati
    Commented Jul 17, 2015 at 10:06
  • This answer is wrong. If the site is in a time zone that has DST, it will only produce the correct result during the DST part of the year.
    – JLRishe
    Commented Nov 10, 2017 at 5:13
  • @JLRishe Is that because the daylightBias will always return a value even if DST is not active?
    – Dinerdo
    Commented Apr 13, 2018 at 22:23
0

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);
-1

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

3
  • He needs a JavaScript/ECMA solution. I don't think your solution wil work that way.:)
    – Taran Goel
    Commented Jul 10, 2015 at 13:40
  • It should work in Javascript also..!! Commented Jul 11, 2015 at 5:34
  • The <%= System.DateTime.Now %> isn't JavaScript.
    – JLRishe
    Commented Nov 10, 2017 at 16:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.