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
    Regular Coder
    Join Date
    Feb 2005
    Posts
    190
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Onload change date format in text box

    Hi there

    This problem sounds easy in my head but I just cant seem to do or find anything which works!

    I have a textbox which has the date in it. The format is DD/MM/YY (ie. 10/09/12).

    Onload, I would like to change the date in the textbox to format "10 September 2012".

    The only reason for this change in date format is purely aesthetics

    Any help appreciated. Thanks all!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,907
    Thanks
    56
    Thanked 544 Times in 541 Posts
    Code:
    var months=["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var bits=document.getElementById("yourtextboxid").value.split("/");
    document.getElementById("yourtextboxid").value=months[bits[1]-1]+" "+bits[0]+" "+"20"+bits[2];

  • Users who have thanked xelawho for this post:

    vorl (09-10-2012)

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Set a window.onload = functionName; or use the jQuery .ready() to contain the code.

    Get the value of the textbox.

    Split the value using / as the delimiter.

    Leave the date as is; set up a switch/case to change the second to the full name (ie case 1: "January", case 2: "February", etc.); add 2000 to the year.

    Repopulate the textbox with the new value.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <html>
    <head>
    </head>
    <body onload = "changedate()">
    <input type = "text" id = "mydate" value = "10/09/12">
    
    <script type = "text/javascript">
    function changedate() {
    var m = ['January','February','March','April','May','June','July','August','September','October','November','December'];
    var ds = document.getElementById("mydate").value.split("/");
    var mth = Number(ds[1])-1;  // array index starts at 0
    var newdate = ds[0] + " " + m[mth] + " " + (Number (ds[2])+2000);
    }
    </script>
    
    </body>
    </html>

    Portuguese Water Dog Pups - adorable mostly black President Obama has one. Deposits now being taken. £680. - Ad in Exchange and Mart
    Last edited by Philip M; 09-10-2012 at 09:43 PM.

    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.

  • Users who have thanked Philip M for this post:

    vorl (09-10-2012)

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    190
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Brilliant thanks guys for the advise!

    Amazingly quick too!


  •  

    Posting Permissions

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