We help IT Professionals succeed at work.

Constantly Updating 24 Hour Server Time Clock

Medium Priority
509 Views
Last Modified: 2013-12-20
Is there a way to show a clock that is contantly updating with the date and/or time from the ColdFusion Server?  I know how to do a client side clock in Javascript, but I need to use the server time.

Thanks.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Hi,
Just do your regular javascript clock

Look at the code and modify the vars for
<cfoutput>
hours=#Timeformat(Now(),"H")#;
minutes=#Timeformat(Now(),"mm")#;
seconds=#Timeformat(Now(),"ss")#;
</cfoutput>
or whatever your current vars are

This will of course need to be in a cfm page and not a js page

CERTIFIED EXPERT

Commented:
I see you had wanted the date also
just do similar using
date=#DateFormat(now())#;
in your script

Author

Commented:
Close but I can't get it to refresh every second.  Once I add the CFOUTPUT tag, the clock stops updating.  Any ideas?

  This is the original code:
================================
<HEAD><cfoutput>
<SCRIPT language="JavaScript">
<!--

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.getYear();
var AorP=" ";

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

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

if (nhours==0)
   nhours=12;

if (nsecn<10)
 nsecn="0"+nsecn;

if (nmins<10)
 nmins="0"+nmins;

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

nmonth+=1;

if (nyear<=99)
  nyear= "19"+nyear;

if ((nyear>99) && (nyear<2000))
 nyear+=1900;

document.getElementById('clock').innerHTML = nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear;

setTimeout('startclock()',1000);

}

//-->
</SCRIPT>
</cfoutput>

</HEAD>
<BODY>
<div id="clock"></div>  
<SCRIPT language="JavaScript">
<!--
startclock();
//-->
</SCRIPT>
</BODY>
================================

with this

================================
<HEAD>
<cfoutput>
<SCRIPT language="JavaScript">
<!--

function startclock()
{

var nhours=#Timeformat(Now(),"hh")#;
var nmins=#Timeformat(Now(),"mm")#;
var nsecn=#Timeformat(Now(),"ss")#;
var nday="#Dateformat(Now(),"ddd")#";
var nmonth=#Dateformat(Now(),"m")#;
var ntoday=#Dateformat(Now(),"dd")#;
var nyear=#Dateformat(Now(),"yyyy")#;
var AorP=" ";

if (nsecn<10)
 nsecn="0"+nsecn;

if (nmins<10)
 nmins="0"+nmins;

nmonth+=1;

document.getElementById('clock').innerHTML = nhours+": "+nmins+": "+nsecn+" "+AorP+" "+nday+", "+nmonth+"/"+ntoday+"/"+nyear;

setTimeout('startclock()',1000);

}

//-->
</SCRIPT>
</cfoutput>

</HEAD>
<BODY>
<div id="clock"></div>  
<SCRIPT language="JavaScript">
<!--
startclock();
//-->
</SCRIPT>
</BODY>
=================================

Commented:
The reason it won't update is because you are calling startclock repeatedly with the same values. What you need to do is move these variables:

var nhours=#Timeformat(Now(),"hh")#;
var nmins=#Timeformat(Now(),"mm")#;
var nsecn=#Timeformat(Now(),"ss")#;
var nday="#Dateformat(Now(),"ddd")#";
var nmonth=#Dateformat(Now(),"m")#;
var ntoday=#Dateformat(Now(),"dd")#;
var nyear=#Dateformat(Now(),"yyyy")#;
var AorP=" ";

outside of the function, and then have startclock() increment the variables accordingly every time it executes. I.E. +1 second, if second == 60 second = 0 and minute += 1, etc.
CERTIFIED EXPERT
Commented:
I looked at your clock and it's not suitable for what you want to do.
It is relying on javascript to provide it with the correct time every time the function is called (every second) Bad ...

I'm posting you a suitable clock - I just enclosed the entire script in output tags

<cfoutput>
<SCRIPT LANGUAGE="JavaScript">
function MakeArrayday(size)
{
  this.length = size;
  for(var i = 1; i <= size; i++)
    this[i] = "";
  return this;
}
function MakeArraymonth(size)
{
  this.length = size;
  for(var i = 1; i <= size; i++)
    this[i] = "";
  return this;
}
var hours;
var minutes;
var seconds;
var timer=null;
function sClock()
{
  hours=#Timeformat(Now(),"H")#;
  minutes=#Timeformat(Now(),"mm")#;
  seconds=#Timeformat(Now(),"ss")#;
  if(timer){clearInterval(timer);timer=null;}
  timer=setInterval("work();",1000);
}
function twoDigit(_v)
{
  if(_v<10)_v="0"+_v;
  return _v;
}
function work()
{
  if (!document.layers && !document.all && !document.getElementById) return;
  var runTime = new Date();
  var dn = "AM";
  var shours = hours;
  var sminutes = minutes;
  var sseconds = seconds;
  if (shours >= 12)
  {
    dn = "PM";
    shours-=12;
  }
  if (!shours) shours = 12;
  sminutes=twoDigit(sminutes);
  sseconds=twoDigit(sseconds);
  movingtime = "Server Time #Dateformat(Now(),"d mmm yy")# "+ shours + ":" + sminutes +":"+sseconds+"" + " " + dn;
  document.getElementById("clock").innerHTML=movingtime;
  if(++seconds>59)
  {
    seconds=0;
    if(++minutes>59)
    {
      minutes=0;
      if(++hours>23)
      {
        location.replace('#getfilefrompath(getbasetemplatepath())#');
        hours=0;
      }
    }
  }
}
sClock();
</script>
<div id="clock"></div>
</cfoutput>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Perfect.  Thanks alot!

After posting my last message, I jumped in the car to run an errand and realized my silly error.  Sometimes I need to walk away to get my thoughts cleared.

Anyway, thanks again!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.