Results 1 to 4 of 4

Thread: Date TextBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Date TextBox

    Hi there, I have got a textbox on my web page that contains a date, i want to be able to change the date when different keys are pressed, for example if the user presses 't' then the date changes to the current date.

    The initial date is in this format dd/mm/yy.

    How do you take the value from th textbox and convert it to a date in javascript..

    Thanks

    Rohan



  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Code:
    <script language="javascript">
     var whatever = new Date(); 
    </script>
    
    <form name=simpleform>
    <input type=text value="13/9/2002" name=somedate>
    <input type=button onClick="javascript:document.simpleform.somedate.value=whatever;">
    </form>

    Manipulate "whatever" however it is required by you.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153
    hi there, thanks for the reply, when i update the textbox the date changes format,

    from

    17/01/2003

    to

    Thu Jan 1 00:00:00 UTC 2004

    is there a way of changing it back

    Thanks

    Rohan

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Problem with code

    <html>
    <head>
    <title>Test</title>
    <script>
    function addZero(vNumber)
    {
    return((vNumber < 10) ? "0" : "") + vNumber
    }
    function change(txtDate)
    {
    key = window.event.keyCode;
    result = new Date(txtDate.value);
    switch(key)
    {
    case 34:
    result.setMonth(result.getMonth() - 1);
    break;
    case 33:
    result.setMonth(result.getMonth() + 1);
    break;
    case 84:
    result = new Date();
    break;
    case 40:
    result.setDate(result.getDate() - 1);
    break;
    case 38:
    result.setDate(result.getDate() + 1);
    break;
    }
    var output = addZero(result.getDate()) + '/' + addZero(result.getMonth()+1) + '/' + result.getFullYear();
    txtDate.value = output;
    return false;
    }
    </script>
    </head>
    <body>
    <form method="POST" action="--WEBBOT-SELF--">
    <p><input type="text" name="T1" size="36" onkeydown="return change(this);" value="01/01/2003"></p>
    </form>
    </body>
    </html>

    there is a problem with the output date, it looks ok but when it is put back in the textbox the next keypress causes an invalid date.

Posting Permissions

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



Click Here to Expand Forum to Full Width