Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code to have running time from server. 1

Status
Not open for further replies.

jlpowell

Technical User
Apr 10, 2001
2
US
I have many JS / applet combos that effectively give me back time, but it isn't consistent because I want to get back server time not client time.

I have code to get server time but does anyone know how I would get this to be a runnable clock (meaning time moves and shows on screen).

Anyone have working code for server side running clock as a servlet or jsp passing info to servlet?

Tx,
Jim
 
Here's what I would do if I were you:

Get the server side time using java. Then initialize the javascript date with this date and have javascript continuously update the clock on the client.

[tt]...
var clock = new Date(<%= java date from server side %>);
...
code to make clock update.
[/tt]


Do you follow my pseudo code? I hope this helped! ;-)
- Casey Winans
 
<script language=&quot;JavaScript&quot;>
<!--
function startclock()
{
var thetime=new Date();
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var nday=thetime.getDay();
var nmonth=thetime.getMonth();
var ntoday=thetime.getDate();
var nyear=thetime.getFullYear();
var AorP=&quot; &quot;;
var i=0;

if (nhours>=12)
AorP=&quot;P.M.&quot;;
else
AorP=&quot;A.M.&quot;;

if (nhours>=13)
nhours-=12;

if (nsecn<10)
nsecn=&quot;0&quot;+nsecn;

if (nmins<10)
nmins=&quot;0&quot;+nmins;

if (nday==0)
nday=&quot;Sunday&quot;;
if (nday==1)
nday=&quot;Monday&quot;;
if (nday==2)
nday=&quot;Tuesday&quot;;
if (nday==3)
nday=&quot;Wednesday&quot;;
if (nday==4)
nday=&quot;Thursday&quot;;
if (nday==5)
nday=&quot;Friday&quot;;
if (nday==6)
nday=&quot;Saturday&quot;;

nmonth+=1;



document.clockform.clockspot.value=nhours+&quot;: &quot;+nmins+&quot;: &quot;+nsecn+&quot; &quot;+AorP+&quot; &quot;+nday+&quot;, &quot;+nmonth+&quot;/&quot;+ntoday+&quot;/&quot;+nyear;

setTimeout('startclock()',1000);
}
//-->
</script>
</p>
<form name=&quot;clockform&quot;>
<p align=&quot;center&quot;><b><font class=PortletHeading3>Current Server
Time is:</font></b>
<p align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;clockspot&quot; size=&quot;39&quot; font class=&quot;PortletText1&quot; style=&quot;background-color:white;&quot;></p>
</form>
</td>
</tr>
<tr>
<td WIDTH=&quot;40%&quot; HEIGHT=&quot;91&quot; align=&quot;center&quot;>
<p align=&quot;center&quot;> <script language=&quot;JavaScript&quot;>
<!--
function startcounter()
{
var time=new Date();
var mins=time.getMinutes();
var secs=time.getSeconds();
var hrs=time.getHours();
var jmins59=59;
var jmins54=54;
var jdown=&quot;Server is Down. Will be back online in &quot;;
var jsecs=60-secs;
if ((mins>=55) && (mins<=59))
jmins2=jmins59-mins;
if (mins<55)
jmins2=jmins54-mins;
if ((mins>=55) && (mins<=59))
document.clockform2.counterspot.value=jdown + jmins2 + &quot; Minutes, &quot; + jsecs + &quot; Seconds&quot;;
else
document.clockform2.counterspot.value=jmins2 + &quot; Minutes, &quot; + jsecs + &quot; Seconds until next bounce.&quot;;

setTimeout('startcounter()',1000);
}
//-->
</script>
</p>
<form name=&quot;clockform2&quot;>
<p align=&quot;center&quot;><font class=PortletHeading3><b>Current Server
Status:</b></font>
<p align=&quot;center&quot;><input type=&quot;text&quot; name=&quot;counterspot&quot; size=&quot;61&quot; font class=&quot;PortletText1&quot; style=&quot;background-color:white; font-family:Aerial;&quot;></p>
</form>
<script language=&quot;JavaScript&quot;>
<!--
startclock();
startcounter();
//-->
</script>

Could you please expand on this code please?/
 
Question: Does this script currently work? If so, just place the server side time where you create a new Date object in your javascript.

[tt]<script language=&quot;JavaScript&quot;>
<!--
function startclock()
{
var thetime=new Date( <%= server_time %> );
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
...
[/tt]

Catch my drift?
I hope this helped! ;-)
- Casey Winans
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top