Hi,
Here is a solution called "High Resolution Digital Clock" that has been around for pretty long time; it shows date/time and is easily customizable. You can see full-working:
DEMO[
^] - refer to the page footer.
Following is a Javascript file that does the job (note: in body section of the page you have to create a
div
element and set its ID corresponding to the variable:
var _tClock = "Clk"
Listing 1. High-Resolution Digital Clock (shows Date/Time)
var _tClock = "Clk";
var _pID="";
StartTopClockOnLoad(StartTopClock);
function StartTopClockOnLoad(tFunc)
{
if (window.addEventListener)
{ window.addEventListener('load', tFunc, false);}
else if (window.attachEvent) {window.attachEvent('onload', tFunc); }
}
function StartTopClock()
{ _pID = window.setInterval('UpdateTopClock()', 100);}
function UpdateTopClock()
{
try
{
(document.getElementById(_tClock)).innerHTML =
DateAndTime('d', 't', '', '', '', '');
}
catch (e) { }
}
function DateAndTime(ShowDate,
ShowTime,
md,
Clock24,
ShowMS,
LocUTC)
{
var yyyy;
var mm;
var dd;
var h;
var m;
var s;
var ms;
var ampm;
var strD;
var strT;
var ret="";
dtNow = new Date();
if (ShowDate!=""){
if (LocUTC=="UTC") {
yyyy =dtNow.getUTCFullYear();
mm =dtNow.getUTCMonth()+1;
dd =dtNow.getUTCDate();
}
else {
yyyy =dtNow.getFullYear();
mm =dtNow.getMonth()+1;
dd =dtNow.getDate();
}
if (mm<10) mm="0"+mm;
if (dd<10) dd="0"+dd;
if (md=="ddmm") {strD= dd + '/' + mm + '/' + yyyy;}
else {strD= mm + '/'+ dd +'/' + yyyy;}
ret=strD + " ";
}
if (ShowTime != "")
{
if (LocUTC=="UTC") {
h =dtNow.getUTCHours();
m =dtNow.getUTCMinutes();
s =dtNow.getUTCSeconds();
ms=dtNow.getUTCMilliseconds();
}
else {
h =dtNow.getHours();
m =dtNow.getMinutes();
s =dtNow.getSeconds();
ms=dtNow.getMilliseconds();
}
if (h<10) h="0"+h;
if (m<10) m="0"+m;
if (s<10) s="0"+s;
if (ShowMS != "") { ms = "." + Math.round(ms / 100 - 0.5); } else {ms = ""; }
if (Clock24=="24") {strT =h+':'+m+':'+s + ms;}
else {
ampm= (h>=12) ? "PM":"AM";
if (h>12) h-=12;
if (h==0) h=12;
strT= h + ':' + m + ':' + s + ms + ' ' + ampm;
}
ret += strT;
}
ret=((LocUTC=="UTC")? "UTC " : "" ) + ret;
return(ret);
}
Pertinent to this case I would recommend to use setInterval() instead of setTimeout(), which require recursion.
Solution can be easily integrated into either HTML, or ASP.NET files. It can be customized for higher resolution, etc. using the flag vars.
Kind regards,
AB