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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Generate system date in text box

    Does anyone know how to generate system date into a text box in a HTML page?

    Hope someone could help me.

    Thanks.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    window.onload = function(){
       document.theForm.theField.value = new Date().toLocaleString();
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks.

    I have tried with this code, but not working. So, could you be more specific?

    And, where should i put this code in?

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    window.onload = function(){
       document.theForm.theField.value = new Date().toLocaleString();
    }
    </script>
    </head>
    <body>
    <form name="theForm">
    <input type="text" name="theField" />
    </form>
    </body>
    </html>
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Generate system date in text box

    Below is some part of my code, could you please check whether anything wrong as it still not working.

    javascript:
    window.onload = function(){
    document.dateform.currdate.value = new Date().toLocaleString();
    }

    Under tag <body>:
    <td><form name="dateform" method="post" action="">
    <input name="currdate" type="text" size="35">
    </form></td>

    This date will be automatically insert into the text box whenthe HTML page has been opened.
    Last edited by apple; 01-05-2005 at 06:29 AM.

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    It doesn't get any simpler then glenn's example. As long as you copy that it'll work, maybe it's something else you have in your code that you have not posted.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Check for multiple onloads. If you have <body onload="theFunc()">, combine it with the window.onload code I posted.
    Code:
    window.onload = function(){
       document.theForm.theField.value = new Date().toLocaleString();
       theFunc();
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could you please check my entire code.
    Attached Files Attached Files
    Last edited by apple; 02-21-2005 at 01:42 AM.

  • #9
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Did you do what was suggested here?
    Quote Originally Posted by glenngv
    Check for multiple onloads. If you have <body onload="theFunc()">, combine it with the window.onload code I posted.
    Code:
    window.onload = function(){
       document.theForm.theField.value = new Date().toLocaleString();
       theFunc();
    }
    Take out any onload events you have in your body tag, and change your function to this:
    Code:
    window.onload = function(){
       document.dateform.currdate.value = new Date().toLocaleString();   
      MM_preloadImages('img/ejournal_close2.gif','img/ejournal_help2.gif','img/ejournal_send2.gif','img/ejournal_save2.gif','img/ejournal_print2.gif');
    }

  • #10
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot and its finally work.

    I have another questions:

    1) Could i make the text box as unselectable as it the system date cannot be deleted and modified.

    2) The system date displayed into the text box is in this format - Wednesday, January 05, 2005 3:49:50 PM. Lets say i want to display in these formats - January 05, 2005 or 01/05/2005, could you give me some idea and what should be done?

    Thanks.

  • #11
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You could make the text box readonly perhaps.
    Change this line:
    <input name="currdate" type="text" size="35">
    to:
    <input name="currdate" type="text" readonly="readonly" size="35">
    About formatting the date, I'll give you a script that kinda shows how I've formatted the date. Now i'm not sure how with glenn's code. So either you can try it how i've done it, or hope glenn comes to the rescue with another way
    Code:
    <html>
    <head>
    	<title></title>
    <script type="text/javascript">
    
    function getCurrentTime()
    {
    var now = new Date();
    var mySecs = now.getSeconds();
    var curHour = now.getHours();
    var curMin = now.getMinutes();
    var day = now.getDay();
    var date = now.getDate();
    var year = now.getFullYear();
    var month = now.getMonth();
    var days = new Array();
    	days[0] = "Sunday";
    	days[1] = "Monday";
    	days[2] = "Tuesday";
    	days[3] = "Wednesday";
    	days[4] = "Thursday";
    	days[5] = "Friday";
    	days[6] = "Saturday";
    var suffix = "AM";
    
    if(mySecs < 10)
    mySecs = "0" + mySecs;
    
    if(curMin < 10)
    curMin = "0" + curMin;
    
    if(curHour == 12 && curMin >= 1)
    {
    	suffix = "PM";
    }
    if(curHour == 24 && curMin >= 1)
    {
    	curHour-= 12;
    	suffix = "AM";
    }
    if(curHour > 12)
    {
    	curHour-= 12;
    	suffix = "PM";
    }
    	document.getElementById('time').innerHTML=(days[day] + " " + (month +1) + "/" + date + "/" + year + " " + curHour + ":" + curMin + ":" + mySecs + " " + suffix); //you could just put this in your text field instead of using inner html...
    
    }
    
    </script>
    </head>
    <body  onload="setInterval('getCurrentTime()', 1000);">
    <div id="time"></div><br>
    </body>
    </html>
    Last edited by Brandoe85; 01-05-2005 at 08:03 AM.

  • #12
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    In mm/dd/yyyy format:
    Code:
    window.onload = function(){
       var today = new Date();
       var mm = today.getMonth() + 1;
       var dd = today.getDate();
       if (mm<10) mm="0"+mm;
       if (dd<10) dd="0"+dd;
       document.dateform.currdate.value = mm + "/" + dd + "/" + today.getFullYear();   
       MM_preloadImages('img/ejournal_close2.gif','img/ejournal_help2.gif','img/ejournal_send2.gif','img/ejournal_save2.gif','img/ejournal_print2.gif');
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #13
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have tried this and its work, but i still can highlight the text box. If i want to disable the highlight on the text box, what should be done?

  • #14
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    <input name="currdate" type="text" readonly="readonly" size="35" onmousedown="return false;">

  • #15
    New Coder
    Join Date
    Jan 2005
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks and i have tried both code that you all posted.

    For glenngv:
    If i want the date in this format - January 05, 2005, what should be done? Could you give me some idea?


    For Brandoe85:
    In the last part of the coding, you have wrote down a comment "you could...". What do you mean by that?

    Code:
    "document.getElementById('time').innerHTML=(days[day] + " " + (month +1) + "/" + date + "/" + year + " " + curHour + ":" + curMin + ":" + mySecs + " " + suffix); //you could just put this in your text field instead of using inner html..."


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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