Any Javascript for display a running clock?

Discussion in 'JavaScript' started by salayhin, Sep 11, 2009.

  1. #1
    Any Javascript for display a running clock?
     
    salayhin, Sep 11, 2009 IP
  2. ohteddy

    ohteddy Member

    Messages:
    128
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #2
    this uses the prototype function '$' which is a helper for the standard js function 'getElementByID'.

    function showClock(element) {

    if ($(element)) {

    var thetime = new Date()

    var hour = thetime.getHours()

    var mins = thetime.getMinutes()

    var secs = thetime.getSeconds()

    var month = thetime.getMonth() + 1

    var day = thetime.getDate()

    var year = thetime.getYear()



    if (secs < 10) secs = '0' + secs

    if (mins < 10) mins = '0' + mins

    if (hour < 10) hour = '0' + hour

    if (day < 10) day = '0' + day

    if (month < 10) month = '0' + month

    if (year <= 99) year = '19' + year

    if ((year > 99) && (year < 2000)) year += 1900



    time = hour + ':' + mins + ':' + secs

    iso_date = year + '-' + month + '-' + day

    $(element).innerHTML = iso_date + ' ' + time

    }

    setTimeout('showClock("' + element + '")', 1000)

    }
     
    ohteddy, Sep 13, 2009 IP
  3. Ashley1

    Ashley1 Peon

    Messages:
    205
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hi this may help you, try this code
    <html>
    <head>
    <script type="text/javascript">
    function startTime()
    {
    var today=new Date();
    var h=today.getHours();
    var m=today.getMinutes();
    var s=today.getSeconds();
    // add a zero in front of numbers<10
    m=checkTime(m);
    s=checkTime(s);
    document.getElementById('txt').innerHTML=h+":"+m+":"+s;
    t=setTimeout('startTime()',500);
    }

    function checkTime(i)
    {
    if (i<10)
    {
    i="0" + i;
    }
    return i;
    }
    </script>
    </head>

    <body onload="startTime()">
    <div id="txt"></div>
    </body>
    </html>
     
    Ashley1, Oct 7, 2009 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Most likely hundreds. Try google.
     
    camjohnson95, Oct 7, 2009 IP