The Object:
Get the server time to show up on the clients browser and update
real time
The Dilemma:
Javascript is a client side scripting language, so the clocks you
see all over the net won't work.
The Solution:
Use Java Servlets (or any server side language) to call the server
time, initialize the javascript server clock, and then use the client
side javascript to update the clock while keeping track of the time
manually.
Code:
import java.util.Date;
.....
Date date = new Date();
.....
out.println(" <SCRIPT type=\"text/javascript\"
language=\"javascript\">");
out.println(" var timer = null");
out.println(" var crnttime = 0");
out.println(" var mstime = 0");
out.println("");
out.println(" function initializeClock() {");
//Initialize the clock using the server time
out.println(" mstime = "+date.getTime()+"");
out.println(" crnttime = new Date("+date.getTime()+")");
out.println(" startClock()");
out.println(" }");
out.println("");
out.println(" function stopClock() {");
out.println(" clearTimeout(timer)");
out.println(" }");
out.println("");
out.println(" function startClock() {");
out.println(" var hours = crnttime.getHours()");
out.println("");
out.println(" var minutes = crnttime.getMinutes()");
out.println(" minutes=((minutes < 10) ? \"0\" : \"\") +
minutes");
out.println("");
out.println(" var seconds = crnttime.getSeconds()");
out.println(" seconds=((seconds < 10) ? \"0\" : \"\") +
seconds");
out.println("");
out.println(" var clock = hours + \":\" + minutes + \":\" +
seconds");
out.println(" document.forms[0].display.value = clock");
out.println("");
out.println(" mstime += 1000");
out.println(" crnttime = new Date(mstime)");
out.println(" timer = setTimeout(\"startClock()\",1000)");
out.println(" }");
out.println(" </SCRIPT>");
out.println("</HEAD>");
out.println("<BODY onload=\"initializeClock()\"
onunload=\"stopClock()\">");
....
//Put this where you want your clock to be...
out.println(" <input type=\"text\" name=\"display\"
size=\"20\"></TD>");
Conclusion:
I hope this helps someone out. It still needs some work (adjusting
for time zones, etc), but its a start! Feel free to email me with any
questions/comments/error reporting/etc...
You can probably get the server's time from one of the HTTP headers
without explicitly requesting it.
If you don't mind the traffic, you could also request the actual current
time from the server once per second.
Er, why would I need a clock set to your server timezone? As I am probably
on the other side of the planet from you your time is, to me, irrelevant.
Cheers
Richard.
Actually, it's not. If you look carefully at the 4Kb of text
you failed to trim in your reply (tut, tut), you will see that
the problem was _solved_ with Java.
--
Andrew Thompson
http://physci.org/
> Er, why would I need a clock set to your server timezone? As I am probably
> on the other side of the planet from you your time is, to me, irrelevant.
i didnt feel it was relevant to bring up my need to the problem, but
ill go ahead and explain... i am building a registration piece for
people that are given a time to register. since everyone will be
registering through one server (located locally for all users), the
best way to do it was to compare their time to the servers, and grant
them access accordingly. well, i thought it would be nice for people
to know what the "current time" was, and they can compare it to their
registration time. since everything would be done locally, then the
server time is relevant...
why cant people in these newsgroups just help others solve the
questions they ask as opposed to questioning their use for it? the
time issue was obviously something i felt i needed... why isnt that
enough?
nino
Using ASP the problem is trivial:
============= test.asp ==================
<div id=div1>.</div>
<script runat=server language=javascript>
ds = new Date();
ds=ds*1;
</script>
<script>
ds= <% =ds %>; // UTC server
dc=new Date();
dc=dc*1; // UTC client
dif = dc-ds; // UTC difference
showtime();
function showtime(){
d=new Date();
document.getElementById("div1").innerHTML=
"Client time:<br>"+ d + "<br><br>";
dd=new Date(d*1+dif);
document.getElementById("div1").innerHTML+=
"Server corrected time:<br>"+ dd;
setTimeout("showtime()",1000);
};
</script>
========================================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
not so trivial apparently, it should be:
dif = ds-dc; // UTC difference
Why can't people who post questions to these newsgroups provide all the
relevant information right up front, in the original post. These newsgroups
are about the web, not just a local area. Anything discussed here is
assumed to be world wide (thats the first two w's in www) and do to with the
internet, unless specifically indicated otherwise.
Now that I see exactly where you are coming from I see my answer should be
changed.
Why do you feel the need to put yet another clock on your viewers screen.
Don't you think they can just look at their system tray, or their wrist, or
the wall?
Cheers
Richard.
"rf" <making...@the.time> wrote in message
news:pAZva.3974$DP4....@news-server.bigpond.net.au...
>
> "Nino Skilj" <nino9...@yahoo.com> wrote in message
> news:b5f5e117.03051...@posting.google.com...
> > "rf" <making...@the.time> wrote in message
> news:<so5va.1000$DP4....@news-server.bigpond.net.au>...
> >
> > > Er, why would I need a clock set to your server timezone?
I wondered the same myself, but..
> > i didnt feel it was relevant to bring up my need to the problem,
...
> Why can't people who post questions to these newsgroups provide all the
> relevant information right up front, in the original post.
Aaah. I suffer this 'what is relevant' problem as well.
In this case, I considered asking Nino why?
But, having never needed to show time on
web pages, I just shrugged and moved on.
__
But when I'm posting, I always need to
make similar decisions about what is
relevant and what is not.
A concise 2K post can turn into a long,
boring 10K nobody reads, very quickly.
__
It seems this one can be put down to a
difference of opinion on how much was
relevant..
:-)