View Full Version : creating live clock using server's date & time

06-20-2004, 04:00 PM
I need to create a live clock which always shows the current time. however the catch here is i need to first grab the date and time from the server. once i get that date & time, my script should start and the clock should start ticking. i have seen the live clock examples here but they all use a new Date() instance. hence it gets the current machine's time.
currently what i am doing is getting the server's date & time in a textbox. but from thereon i have nothing much to do. in the examples given here i dont have to do much. i just set the interval to 1000 (1 sec) and a new Date() will do the job for me. but when i grab the date from the value i fetched from server (or for that matter, any value i set in the textbox) the complexity increases. i need to increment the value of sec, minutes and hour every second, accordingly and then in turn the dates too !!!
i hope i m clear in the problem here !!!
can anyone help me in this situation...i need to keep the current time (& date) synchronized with the server's date & time (values in textbox).

thanks in advance


06-20-2004, 05:33 PM
You could set a variable to hold the difference, in milliseconds, between the server time and the local time. Then, each time you update the clock, get the client's time and add the constant difference.

Hope that helps!

Happy coding! :)

06-20-2004, 08:27 PM
this link might help some? - got examples of server clocks etc


06-21-2004, 05:46 AM
Put the server date and time in a js variable to create a Date object and then alter it every second. The sampe server-side code I used is VBScript (ASP).

var serverDate;
function getServerDate(){
serverDate=new Date("<%=Now()%>"); //server date and time, change server-side code accordingly

function tick(){
var min = serverDate.getMinutes();
if (min<10) min="0"+min;
var sec = serverDate.getSeconds();
if (sec<10) sec="0"+sec;
window.status = serverDate.getHours() + ":" + min + ":" + sec;

setInterval("tick()", 1000);

06-22-2004, 08:39 AM
not only i solved my problem, I have too many options now :thumbsup:

thanks once again