Results 1 to 5 of 5

Thread: Time in a input

  1. #1
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default Time in a input

    Does anyone know how to make the current time display in a text input???
    I want it in a input! Thanks!

    REALLY APPRECIATE IT!

  2. #2
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Input Clock</title>
    <script type="text/javascript">
    function st() {
    	var c = document.getElementById("clock");
    	var date = new Date(),
    		hr = date.getHours(),
    		m = date.getMinutes(),
    		s = date.getSeconds();
    	var mS = m < 10 ? "0" : "",
    		sS = s < 10 ? "0" : "";
    	var hh = hr > 12 ? hr-12 : hr;
    	c.value = hh+":"+mS+m+":"+sS+s
    	setTimeout(function() {st();},1000);
    	}
    window.onload=st;
    </script>
    </head>
    <body>
    <input id="clock">
    </body>
    </html>
    Last edited by mburt; 03-31-2007 at 06:43 PM.
    - Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't think he wanted it updated, if he did there wouldn't be much point in putting it in an input.
    Code:
    <input type="text" id="clock">
    <script type="text/javascript">
      Number.prototype.pad = function(n) {
        for(var r = this.toString(); r.length < n; r = "0" + r);
        return r;
      };
      var d = new Date();
      document.getElementById("clock").value = [d.getHours().pad(2), d.getMinutes().pad(2), d.getSeconds().pad(2)].join(":");
    </script>
    Code:
            setTimeout("st()",1000);
    That's very ugly, not least because you have to get a reference to the clock all over again. Use a closure:
    Code:
      function updateClock(cl) {
        var d = new Date();
        (cl || cl = document.getElementById("clock")).value = [d.getHours().pad(2), d.getMinutes().pad(2), d.getSeconds().pad(2)].join(":");
        setTimeout(function() { updateClock(cl); }, 1000);
      }
      updateClock();
    Last edited by Twey; 03-31-2007 at 04:24 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jan 2007
    Location
    The stage
    Posts
    568
    Thanks
    23
    Thanked 6 Times in 6 Posts

    Default

    I don't care if it updates, thanks Mb thats gonna help alot. (people have been sending in a lot of forms in to little time (clogging up my email)

  5. #5
    Join Date
    Feb 2007
    Posts
    601
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thats a little harsh don't you think .... ( you didn't have to say it like that )

Bookmarks

Posting Permissions

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