Get Server Time and pass to Javascript

calvin

Regular
Joined
Nov 6, 2003
Messages
71
Hi,

I'm doing an attendance system and face a problem to get the server time show in javascript. It was an error since the server time is not same as the local time. This problem occurs when user click the button and get server time add to database, it is no problem save the time to database, but it will confuse :confused: the user if local time is not same as the server time.

Anyone have an idea or snippets to solve the problem. I got a lot of sample code of javascript by all get in local time. I need it get from server time. A lot of people said javascript cannot do such function, but i believe that there are another approach to solve such problem.

For those comments and suggestions will more appreciate. :)

Calvin
 
Best thing to do is to ask the user for their timezone, and then you convert the time based on the server's timezone. Do not rely on javascript time.
 
As kahlua001 said; ask the user for their timezone. Also, if there is a login process or some sort of user profile setup for each user you can save their timezone as an integer from GMT.
 
Got Solution

Code behind
Response.Write("<script>var timeServer = new Date('" + DateTime.Now.ToString() + "');</script>");

Client Side code
<script type="text/javascript">

//Users time
var timeLocal = new Date();

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

//initalize 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>
 
Back
Top