Some time ago I wrote post Get Sharepoint current datetime in javascript for Sharepoint Online, where described the way how to get current server datetime in javascript and how it works (or to be more precise almost current date time – see below for explanation). Briefly we use _spPageContextInfo.clientServerTimeDelta standard variable which is returned from server in the following form:
1: clientServerTimeDelta: new Date("2015-05-21T16:54:00.0000000Z") - new Date()
In this line of code string is populated from DateTime.Now on server side:
1: sb.Append("\", clientServerTimeDelta: new Date(\"");
2: sb.Append(DateTime.UtcNow.ToString("o", CultureInfo.InvariantCulture));
3: sb.Append("\") - new Date()");
So in order to get current server datetime in javascript we need to add current javascript date to _spPageContextInfo.clientServerTimeDelta. In original post I wrote that it can be done like that:
1: var d = new Date();
2: var currentServerDateTime = _spPageContextInfo.clientServerTimeDelta + d;
which is not fully correct. In order to have correct datetime object in currentServerDateTime use the following code:
1: var currentServerDateTime = new Date(new Date().getTime() + _spPageContextInfo.clientServerTimeDelta);
And remember that it will be almost current server date time – difference will be in the time between moments when javascript interprets line with initialization of _spPageContextInfo.clientServerTimeDelta variable (see above) and line where you calculate currentServerDateTime.