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 11 of 11

Thread: Time in textbox

  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Time in textbox

    Hey something really simple that I just cant figure out.

    I want to insert the time into a textbox how do I do that, I'm just learning javascript so I'm not sure of how to set it up!!

    Thanks


    Oh and also I have two checkboxes in my form and if one is checked i want to make it so the other cannot be checked. How would I do that without naming the two checkboxes the same. I cant use radio buttons because the program it is interfacing with does not have radio buttons.

    Thanks again

  • #2
    New Coder
    Join Date
    Oct 2004
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try this:

    PHP Code:
    <html>
    <
    head>
    <
    script type="text/javascript">
    function 
    TickTick()
    {
    var 
    today=new Date()
    var 
    dd today.getDate();
    var 
    mm today.getMonth();
    var 
    yy today.getFullYear();
    var 
    h=today.getHours()
    var 
    m=today.getMinutes()
    var 
    s=today.getSeconds()
    m=secTicker(m)
    s=secTicker(s)

    document.getElementById('Clock').value="Time="+h+":"+m+":"+s;
    t=setTimeout('TickTick()',1000)

    }

    function 
    secTicker(i)// This Function is to add zero before minutes and second value which is less than 10
    {
    if (
    10){"0" i}return i; }
    </script>
    </head>

    <body onload="TickTick()">

       <input type="text" id="Clock"name="Clock">
    </body>
    </html> 

  • #3
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Cool it works except for I need a 12 hour format not a 24 hour format

    I appreciate your help and anymore help that is given on this subject

  • #4
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If I try to erase "Time="
    document.getElementById('Clock').value="Time="+h+":"+m+":"+s;

    or take out the parentheses it doesnt work, I'm not quite understanding why this is isnt "time" the object its calling??
    Last edited by toddn; 01-03-2008 at 10:22 PM.

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post
    I erased "Time="..see if it works ?


    Code:
    <script type="text/javascript"> 
    function TickTick() 
    { 
      var today=new Date() 
    
    var h=today.getHours() 
    var m=today.getMinutes() 
    var s=today.getSeconds() 
    m=secTicker(m) 
    s=secTicker(s) 
    
    document.getElementById('Clock').value=h+":"+m+":"+s; 
    t=setTimeout('TickTick()',1000) 
    
    } 
     function secTicker(i)
    { 
    if (i < 10){i = "0" + i}return i; } 
    
    </script> 
    </head> 
    
    <body onload="TickTick()"> 
    
       <input type="text" id="Clock"name="Clock">

  • Users who have thanked gencoglu4 for this post:

    toddn (01-03-2008)

  • #6
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I thought i did that i mustve left the " before h+, thanks for that. If i wanted to get it to show say 2:34 pm instead of 14:34 what part of the code am i supposed to be looking at??

    I guess I dont understand where it is getting the time from in that format, can someone also explain it??
    Last edited by toddn; 01-03-2008 at 10:38 PM.

  • #7
    New Coder
    Join Date
    Dec 2007
    Posts
    14
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Remember that the time you are getting is the client-side time, i.e. the time of the user PC.

    Maybe the clock of the client may be wrong, or it may be set on another time-zone.

    Think about using server side scripting (such as PHP) to retrieve the time

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by toddn View Post
    I thought i did that i mustve left the " before h+, thanks for that. If i wanted to get it to show say 2:34 pm instead of 14:34 what part of the code am i supposed to be looking at??

    I guess I dont understand where it is getting the time from in that format, can someone also explain it??
    <script type="text/javascript">
    function TickTick() {
    var today=new Date();;

    var h=today.getHours();
    var h1 = h;
    var m=today.getMinutes();
    var s=today.getSeconds();
    var ampm = " AM";

    if (h >= 12) {ampm = " PM"}
    if (h >= 13) {h = h - 12}
    if (h1 == 0) {h = 12} // i.e. 12.xx am

    h = secTicker(h); // if zero before hours required
    m = secTicker(m);
    s = secTicker(s);

    document.getElementById('Clock').value = h + ":" + m + ":"+ s + ampm;
    t=setTimeout('TickTick()',1000);
    }

    function secTicker(i) {
    if (i < 10) {i = "0" + i}
    return i;
    }

    </script>
    </head>

    <body onload = "TickTick()">
    <input type = "text" id = "Clock" name = "Clock">

    Your second question:-
    The whole point of checkboxes is that more than one can be checked, while only one radio of a set may be selected. Another way is to use options. But if you insist:-

    <form name = "myform" onsubmit = "return checkChecks()">
    <input type = "checkbox" name = "chk1" value = "First">FIRST<br>
    <input type = "checkbox" name = "chk2" value = "Second">SECOND<br><br>
    <input type = "submit" value = "SUBMIT">
    </form>

    <script type = "text/javascript">

    function checkChecks() {
    if ((document.myform.chk1.checked) && (document.myform.chk2.checked)) {
    alert ("You may only check ONE checkbox");
    return false;
    }
    }

    </script>
    Last edited by Philip M; 01-04-2008 at 09:31 AM. Reason: Second question anaswered

  • #9
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    unfortunately that wasnt quite what i was looking for, i think i'm looking more for a boolean function, if a = true then b = false i just dont know how to code it

    <tr><td class=m>Check In</td>
    <td class=m><input type=checkbox size=40 name=_fid_28 class=m> Check Out</td>
    <td class=m><input type=checkbox size=40 name=_fid_29 ></td>


    this is my code

  • #10
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts
    basically i need it to act like a radio button, if one is checked then the other unchecks

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    <form name = "myform" >
    <input type = "checkbox" name = "chk1" value = "First" onclick= "checkChecks1()">FIRST<br>
    <input type = "checkbox" name = "chk2" value = "Second" onclick = "checkChecks2()">SECOND<br><br>
    </form>

    <script type = "text/javascript">

    function checkChecks1() {
    if (document.myform.chk2.checked) {
    document.myform.chk2.checked = false;
    alert ("You may only check ONE checkbox"); /// delete as required
    return false;
    }
    }
    function checkChecks2() {
    if (document.myform.chk1.checked) {
    document.myform.chk1.checked = false;
    alert ("You may only check ONE checkbox"); // delete as required
    return false;
    }
    }
    </script>

    It is your responsibility to die() if necessary….. - PHP Manual


  •  

    Posting Permissions

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