-1

I have an input (datepicker)

<input runat="server" id="txtdob" />
<asp:Button runat="server" ID="btnsubmit" Text="Submit" OnClick="btnsubmit_Click" ></asp:Button>

but when i enter a date (not from calender) format 26-06-1993 I get an error because month and date are replace on click on submit button then i am using a Javascript function to convert date format on focusout.

Javascript

<script>
  $(document).ready(function () {
    $("#txtdob").focusout(function () {
      debugger;
      var input_date2 = document.getElementById("txtdob").value;
      debugger;

      var myDate = new Date(input_date2);
      var prettyDate =
        ('0' + (myDate.getDate())).slice(-2) + '-' +
        ('0' + (myDate.getMonth() + 1)).slice(-2) + '-' +
        myDate.getFullYear();
      document.getElementById("txtdob").value = prettyDate;
      document.write(prettyDate);
    });
  });
</script>

but here i am facing the same problem like when I enter DOB = 06-26-1993 everything is working properly and date is also convert into 26-06-1993 format but when I enter DOB = 26-06-1993 I get the same error. How to solve it ?

rrd
  • 5,789
  • 3
  • 28
  • 36
aparna rai
  • 823
  • 10
  • 24

2 Answers2

0

I understand Your Problem make Sure This Will Help You

  <script>
        var YourStringValue= function (date, format) {
            debugger;
            var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            getPaddedComp = function (comp) {
                return ((parseInt(comp) < 10) ? ('0' + comp) : comp)
            },
            formattedDate = format,
            o = {
                "y+": date.getFullYear(), // year
                "M+": months[date.getMonth()], //month
                "d+": getPaddedComp(date.getDate()), //day
                "h+": getPaddedComp((date.getHours() > 12) ? date.getHours() % 12 : date.getHours()), //hour
                "H+": getPaddedComp(date.getHours()), //hour
                "m+": getPaddedComp(date.getMinutes()), //minute
                "s+": getPaddedComp(date.getSeconds()), //second
                "S+": getPaddedComp(date.getMilliseconds()), //millisecond,
                "b+": (date.getHours() >= 12) ? 'PM' : 'AM'
            };

            for (var k in o) {
                if (new RegExp("(" + k + ")").test(format)) {
                    formattedDate = formattedDate.replace(RegExp.$1, o[k]);
                }
            } debugger;
            return formattedDate;
        };
        $(function () {

            debugger;
            var tst = '26-06-1993';
            var res = tst.split("-");
            try { 
                var formattedDate = YourStringValue(new Date(res[1] + '-' + res[0]+'-' + res[2]), "d-M-y")
                alert(formattedDate);
            }
            catch (err) {
                alert('FormtString');
            }

        });
    </script>
Naveen
  • 294
  • 1
  • 11
  • Code–only answers aren't that helpful. A good answer should include why the OP has their issue and how your code fixes it. There is no need for *parseInt* in `(parseInt(comp) < 10)`, and the milliseconds aren't padded correctly. Your string parse is flawed, never use the built-in parser for non-standard string formats, and the idea of converting a string to date, then formatting it back into a string is unreliable and inefficient. The use of *try..catch* is gratuitous. – RobG Aug 14 '18 at 08:58
0

For this type of issue, there is Date.js, which convert the value automatically.

Just check in that page with example, so no need to create function, just add in your page and use it as you required.

so if you are entered as 20-08-2018 or 20/08/2018 that show August 20, 2018

Ajay2707
  • 5,690
  • 6
  • 40
  • 58