Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-20-2004, 04:00 PM   PM User | #1
hemaltrivedi
New to the CF scene

 
Join Date: Jun 2004
Location: Copenhagen
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
hemaltrivedi is an unknown quantity at this point
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
hemaltrivedi is offline   Reply With Quote
Old 06-20-2004, 05:33 PM   PM User | #2
nolachrymose
Regular Coder

 
Join Date: Jun 2002
Posts: 338
Thanks: 0
Thanked 0 Times in 0 Posts
nolachrymose is an unknown quantity at this point
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!
nolachrymose is offline   Reply With Quote
Old 06-20-2004, 08:27 PM   PM User | #3
homerUK
Regular Coder

 
Join Date: Nov 2002
Location: Manchester, UK
Posts: 533
Thanks: 4
Thanked 1 Time in 1 Post
homerUK is an unknown quantity at this point
this link might help some? - got examples of server clocks etc

http://www.bloke.com/javascript/Clock/server.html
homerUK is offline   Reply With Quote
Old 06-21-2004, 05:46 AM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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);
}
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 06-22-2004, 08:39 AM   PM User | #5
hemaltrivedi
New to the CF scene

 
Join Date: Jun 2004
Location: Copenhagen
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
hemaltrivedi is an unknown quantity at this point
Thanks a lot guys.

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

thanks once again
hemaltrivedi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:07 AM.


Advertisement
Log in to turn off these ads.