Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2004
    Location
    Copenhagen
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    creating live clock using server's date & time

    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

    Hemal

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    this link might help some? - got examples of server clocks etc

    http://www.bloke.com/javascript/Clock/server.html

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    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).
    Code:
    var serverDate;
    function getServerDate(){
       serverDate=new Date("<%=Now()%>"); //server date and time, change server-side code accordingly
    }
    
    function tick(){
       serverDate.setSeconds(serverDate.getSeconds()+1);  
       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;
    }
    
    window.onload=function(){
       getServerDate();
       setInterval("tick()", 1000);
    }

  • #5
    New to the CF scene
    Join Date
    Jun 2004
    Location
    Copenhagen
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks a lot guys.

    not only i solved my problem, I have too many options now

    thanks once again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •