calvin Posted May 19, 2005 Posted May 19, 2005 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 Quote
kahlua001 Posted May 19, 2005 Posted May 19, 2005 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. Quote
Moderators Robby Posted May 19, 2005 Moderators Posted May 19, 2005 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. Quote Visit...Bassic Software
calvin Posted May 25, 2005 Author Posted May 25, 2005 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> Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.