locked
Date Set My Format In Javascript on Change RRS feed

  • Question

  • User-807418713 posted

    Hello

    I Have Textbox1 In Asp.net C#

    In Textbox user will enter date like this format

    01.11.1986 on change i want to show 01-Nov-1986

    How To Do So..?

    Sunday, April 14, 2019 5:37 AM

Answers

  • User839733648 posted

    Hi Gopi.MCA,

    According to your description, I suggest that you could read the input text and convert it to the format you want.

    I've made a sample and maybe you could refer to.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script>
            function TestChange(a) {
                var b = document.getElementById(a).value;
                var day = b.split('.')[0];
                var mon = b.split('.')[1];
                var year = b.split('.')[2];
                var monthName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
                var mont = monthName[mon - 1];
                var c = day.concat('-', mont, '-', year);
                document.getElementById(a).value = c;
            }
    
        </script>
    </head>
    <body>
        <input type="text" id="testid" onchange="TestChange(this.id)" />
    </body>
    </html>

    Result:

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 15, 2019 3:10 AM

All replies

  • User438705957 posted

    I recommend using the Moment library for anything Date related.

    I'm sure you can parse your forms date field in the format you mentioned, and then reformat that to your required format.
    Of course you will have to setup a listener for the change event.

    $('#frmUpdate').on("change""#yourDateField"function () {
    	var newDate = moment(@YourDateField, "DD.MM.YYYY").format("DD-MMM-YYYY");
    $('#yourDateField')= newDate;
    });
    Monday, April 15, 2019 2:45 AM
  • User839733648 posted

    Hi Gopi.MCA,

    According to your description, I suggest that you could read the input text and convert it to the format you want.

    I've made a sample and maybe you could refer to.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script>
            function TestChange(a) {
                var b = document.getElementById(a).value;
                var day = b.split('.')[0];
                var mon = b.split('.')[1];
                var year = b.split('.')[2];
                var monthName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
                var mont = monthName[mon - 1];
                var c = day.concat('-', mont, '-', year);
                document.getElementById(a).value = c;
            }
    
        </script>
    </head>
    <body>
        <input type="text" id="testid" onchange="TestChange(this.id)" />
    </body>
    </html>

    Result:

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 15, 2019 3:10 AM