SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Inserting time

  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inserting time

    want a button on my website which when clicked inserts the current time in corresponding textarea.

    Please note that i dont want to keep showing the current time. I want the current time to be inserted in the textbox when the button is clicked.



    Here is the code what i tried. See if this can be of any help...


    <form name = "myForm">
    <input type = "button" value = "Click To Insert Current Time" onclick = "setClock()">
    <input type = "text" name = "timeDisplay">

    <script type="text/javascript">
    function setClock() {
    var today = new Date();
    var hh = today.getHours();
    if (hh <10) {hh = "0" + hh};
    var mm = today.getMinutes();
    if (mm <10) {mm = "0" + mm};
    var ss = today.getSeconds();
    if (ss <10) {ss = "0" + ss};
    var time = hh + ":" + mm + ":" + ss;
    document.myform.timeDisplay.value = time;
    }
    </script>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function setClock() {
        document.myform.timeDisplay.value = Date().match(/\s..:..:..\s/);
    }

    The Date() results in something like
    Fri May 02 2008 00:07:39 GMT+1200 (New Zealand Standard Time)

    and the \s..:..:..\s restricts it to 00:07:39
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •