• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

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.
0
SiriusPhil
Asked:
SiriusPhil
  • 3
  • 2
1 Solution
 
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now