Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Display Clock in an input box

    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

    Code:
    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();

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    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:
    Code:
    function dT()
    { 
        document.form.date.value=eval(oT); 
        setTimeout('dT()',1000); 
    }

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    Here's the same code, really, but hopefully more readable:
    Code:
    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;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •