...

View Full Version : Display Clock in an input box



Papajo
06-29-2009, 04:43 PM
Hi
This script displays a time and date using a document write tag, I want to display it in a input form box, I tried using this tag but just don't know how to write it: document.form.date.value=""

<form name='form'>
<input type='text' name='date' size='30'>
</form>

Thanks Joe



function tS(){ x=new Date();
x.setTime(x.getTime()); return x; }
function lZ(x){ return (x>9)?x:'0'+x; }
function tH(x){ if(x==0){ x=12; }
return (x>12)?x-=12:x; }
function y2(x){ x=(x<500)?x+1900:x;
return String(x).substring(2,4) }
function dT(){ if(fr==0){ fr=1;
document.write('<font size=2 face=Arial><b><span id="tP">'+eval(oT)+'</span></b></font>'); }
document.getElementById('tP').innerHTML=eval(oT);
setTimeout('dT()',1000); }
function aP(x){ return (x>11)?'pm':'am'; }
var dN=new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'),mN=new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),fr=0,oT="dN[tS().getDay()]+' '+tS().getDate()+' '+mN[tS().getMonth()]+' '+y2(tS().getYear())+' '+':'+':'+' '+tH(tS().getHours())+':'+lZ(tS().getMinutes())+':'+lZ(tS().getSeconds())+' '+aP(tS().getHours())";
dT();

Old Pedant
06-29-2009, 11:04 PM
Geez is that ugly and obscure code. Some people just enjoy writing things to be unreadable, I guess.

But anyway, *PROBABLY* all you need to do is change that dT() function:


function dT()
{
document.form.date.value=eval(oT);
setTimeout('dT()',1000);
}

Old Pedant
06-29-2009, 11:18 PM
Here's the same code, really, but hopefully more readable:


var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
// NN: always return 2 digits, pad with 0 if needed.
function NN(val)
{
return val > 9 : val : "0" + val;
}
function getClock( )
{
var dt = new Date();
var h = dt.getHours();
// get the displayable hour 0 goes to 12 am, 13 thru 23 go to 1 to 11 pm
var hr = h > 12 ? h-12 : ( h == 0 ? 12 : h );
// pm is any time noon or later
var ampm = h >= 12 ? " pm" : " am";
// and then just build up the string. You could easily alter this
// to use only the pieces of the date that you actually want
var clock =
days[dt.getDay()] + " "
+ dt.getDate() + " " + months[dt.getMonth()] + " " + dt.getFullYear()
+ " :: " + hr + ":" + NN(dt.getMinutes()) + ":" + NN(dt.getSecontds) + ampm;
setTimeout("getClock()", 1000);
return clock;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum