Source code to get the server time from client side JavaScript code in asp.net or SharePoint application. Below sample code can be modified based on requirement.

 

<script type="text/javascript">

//Users time
var timeLocal = new Date();
//Servers time
var timeServer = new Date("<%= DateTime.Now %>");

//Calculate the difference (returns milliseconds)
millDiff = timeLocal - timeServer;

//initialize the clock on loading of page
window.onload=function(){
//set the interval so clock ticks
var timeClock=setInterval("TimeTick()",10);
}

//The ticking clock function
function TimeTick(){
//grab updated time
timeLocal = new Date();
//add time difference
timeLocal.setMilliseconds(timeLocal.getMilliseconds() - millDiff);
//display the value
document.getElementById("spanTime").innerHTML =timeLocal;
}

</script>

<span id="spanTime"></span>

Similar Topics: