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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Button date insert

    Hi all, very new to Javascript, been searching the net for a simple script which has a text box with a button beside and when the visitor clicks the button the date is enter in to the text box automaticly.

    Easy, I guess if you know how.

    Thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Like so:-

    Code:
    <input type = "button" value = "Get today's date" onclick = "getTheDate()">
    <input type = "text" id = "todayDate" readonly">
    
    <script type = "text/javascript">
    function getTheDate() {
    document.getElementById("todayDate").value = new Date().toDateString();
    }
    </script>

    Quizmaster: Which ancient Greek poet is also the first name of a main character in "The Simpsons"?
    Contestant: Bart

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Fantastic worked perfectly.

    Thanks so much.

  • #4
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How can i display the date as YYYY-mm-dd?

    Thanks again

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by duncan121 View Post
    How can i display the date as YYYY-mm-dd?

    Thanks again
    Better if you ask for what you want at the outset.

    Code:
    <input type = "button" value = "Get today's date" onclick = "getTheDate()">
    <input type = "text" id = "todayDate" readonly">
    
    <script type = "text/javascript">
    function getTheDate() {
    var today = new Date() ;
    var yr = today.getFullYear();
    var mth = today.getMonth()+1;  // months in Javascript are 0-11
    if (mth <10) {mth = "0" + mth}
    var dt = today.getDate();
    if (dt <10) {dt = "0" + dt}
    // you can now display the date in any format you require
    var theDate = yr + "-" + mth + "-" + dt;
    document.getElementById("todayDate").value = theDate;
    }
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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