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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing seconds in a clock 1

Status
Not open for further replies.

ScottNeth

Technical User
Mar 11, 2002
44
US
Ok, this is more something that's been irritating me more than soemthing that's essential - in ColdFusion, how do you show the precise time where the seconds change w/out having to reload the browser? I've got the
<cfset todayDate=Now()>
<cfoutput> #DateFormat(TodayDate, &quot;mmm dd yyyy&quot;)# </cfoutput><cfoutput>#TimeFormat (todayDate, &quot;hh:mm:sstt&quot;)#</cfoutput>
stuff figured out because its pretty elemental, but how on earth do I get the seconds to refresh?!? It's driving me nuts. I tried using flush but I must not have been doing it correctly or something because I'm not the greatest at hand coding. Scott Neth
Web Designer/Cyberpunk
 
Hi,

I have used a javascript function which you could probably steal from; this self-refreshes every second so the time 'appears' to be live. You're welcome to the code, see below.

M

<SCRIPT>
<!--
/*
written by Simon Brazil for Corporate Internet Design
*/
var __clock_astrMonths = new Array(&quot;Jan&quot;,&quot;Feb&quot;,&quot;Mar&quot;,&quot;Apr&quot;,&quot;May&quot;,&quot;Jun&quot;,&quot;Jul&quot;,&quot;Aug&quot;,&quot;Sep&quot;,&quot;Oct&quot;,&quot;Nov&quot;,&quot;Dec&quot;);
var __clock_astrDays = new Array(&quot;Sun&quot;, &quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;);

function clock_getTime(i_bHideSeconds) {
var Digital=new Date();
var hours=Digital.getHours();
var minutes=Digital.getMinutes();
var seconds=Digital.getSeconds();
var dn=&quot; AM&quot;;
if (hours>12){
dn=&quot; PM&quot;;
hours=hours-12;
}
if (hours==0) hours=12;
if (minutes<=9) minutes=&quot;0&quot;+minutes;
if (seconds<=9) seconds=&quot;0&quot;+seconds;
var strDate = __clock_astrDays[Digital.getDay()] + &quot; &quot; + Digital.getDate() + &quot; &quot; + __clock_astrMonths[Digital.getMonth()] + &quot; &quot; + Digital.getFullYear();
return strDate + &quot; &quot; + hours + &quot;:&quot; + minutes + ((i_bHideSeconds) ? &quot;&quot; : &quot;:&quot; + seconds) + dn;
}

function clock_showClock(){
var tme = clock_getTime();
if (document.getElementById)
{ //write updated date/time (including seconds) to DHTML browser
document.getElementById(&quot;divClock&quot;).innerHTML = '<FONT face=&quot;Arial, Helvetica, sans-serif&quot; size=1 color=&quot;#999999&quot;>' + tme + '</font>';
} else {
if(document.all) {
// If IE4.0
document.all(&quot;divClock&quot;).innerHTML = '<FONT face=&quot;Arial, Helvetica, sans-serif&quot; size=1 color=&quot;#999999&quot;>' + tme + '</font>';
} else {
// If Netscape
document.layers[&quot;divClock&quot;].document.open();
document.layers[&quot;divClock&quot;].document.write(&quot;<table border='0' width='150' height='20' cellpadding='0' cellspacing='0'><tr>&quot;);
if(document.bgColor.toLowerCase()==&quot;#ffffff&quot;) {
document.layers[&quot;divClock&quot;].document.write(&quot;<td valign='top' align='right'><font face='arial' color='#999999' style='font-size:8.5pt'>&quot; + tme + &quot;</font></TD></TR></table>&quot;);
} else {
document.layers[&quot;divClock&quot;].document.write(&quot;<td valign='top' align='right'><font face='arial' color='#993399' style='font-size:8.5pt'>&quot; + tme + &quot;</font></TD></TR></table>&quot;);
}
document.layers[&quot;divClock&quot;].document.close();
}
}
}
if (document.getElementById || document.all) {
document.write ('<DIV NAME=&quot;divClock&quot; ID=&quot;divClock&quot; style=&quot;position:absolute;left:596;top=187;width:155&quot;><FONT face=&quot;Arial, Helvetica, sans-serif&quot; size=1 color=&quot;#999999&quot;>' + clock_getTime() + '</font></DIV>');
//Update Clock each second...
setInterval(&quot;clock_showClock()&quot;,1000);
} else {
document.write(&quot;<LAYER bgcolor='&quot; + document.bgColor + &quot;' id='divClock' name='divClock' left='600' top='187' width='155' height='20'>&nbsp;</LAYER>&quot;);
setInterval(&quot;clock_showClock()&quot;,1000);
//document.write (clock_getTime(true));
}


//-->
</SCRIPT>
&quot;There are 3 kinds of people; those that can count and those that can't&quot;
 
Nice...very nice. I tweaked the layer size a little and added a color to match the rest of the page and its perfect. Scott Neth
Web Designer/Cyberpunk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top