Constantly Updating 24 Hour Server Time Clock

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.
SiriusPhilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dgrafxCommented:
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

0
dgrafxCommented:
I see you had wanted the date also
just do similar using
date=#DateFormat(now())#;
in your script
0
SiriusPhilAuthor 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>
=================================
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

umbraeCommented:
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.
0
dgrafxCommented:
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>

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SiriusPhilAuthor 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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.