javascript date format into dd/mm/yyyy -


i have bit of code:

<body>     <form id="form1" runat="server">     <div>         <asp:textbox runat="server" id="textbox1" text="10/20/2013" onchange="javascript:myfunc();"></asp:textbox>         <asp:textbox runat="server" id="textbox2" text=""></asp:textbox>     </div>     </form>     <script type="text/javascript">         function myfunc() {             mytextbox = document.getelementbyid("<%= textbox1.clientid %>");             mytextbox2 = document.getelementbyid("<%= textbox2.clientid %>");             var date = new date(mytextbox.value);             var day = date.getdate();             var month = date.getmonth() + 1;             var year = date.getfullyear() + 1;             mytextbox2.value = day + "/" + month + "/" + year;         }     </script> </body> 

which 2 textboxes , when first textbox date updated second textbox value becomes date textbox 1 + 1 year.

the code works fine except 1 issue. in textbox1 date must in format mm/dd/yyyy wrong. example if want change 20/10/2013 20/10/2014 must enter 10/20/2013 in first textbox.

how can work dd/mm/yyyy?

the date constructor can't directly take dd/mm/yyyy format, have parse date input:

var temp = mytextbox.value.split('/'); var date = new date(temp[2], temp[1]-1, temp[0]); 

the above parses day, month , year text input, passes each value date constructor. works because 1 of overloads constructor. note month 0 based, need decrement 1.

from mdn:

new date(year, month, day [hour, minute, second, millisecond]);


Comments