convert mm/dd/yyyy to dd/mm/yyyy using javascript while adding Number of days with a Date -
this question has answer here:
i want add number of days particular date. have done problem works mm/dd/yyyy format. want work dd/mm/yyyy format. below sample code..
<script type="text/javascript"> function datechange() { var d = document.getelementbyid('days').value; var mydate = new date(document.getelementbyid('date1').value); mydate.setdate(mydate.getdate() + parseint(d)); document.getelementbyid('date2').value = (mydate.getdate() + 1 ) + '/' + (mydate.getmonth() + 1) + '/' + (mydate.getfullyear()); } function adddate() { date = new date(); var month = date.getmonth() + 1; var day = date.getdate(); var year = date.getfullyear(); if (document.getelementbyid('date1').value == '') { document.getelementbyid('date1').value = month + '/' + day + '/' + year; } } </script>
and html code is--
<body onload="adddate()"> <br/> number of days add : <input type="text" id="days" onchange= "datechange()" /> <br/> <br/> today's date (mm / dd / yyyy) : <input type="text" id="date1" onchange="datechange()" /> <br/> <br/> (mm / dd / yyyy) : <input type="text" id="date2" readonly/> </body>
thanks in advance
as pointed out in comments, think there enough info on duplicate questions solve this. in case looking specific code sample, suggestion split 'mm/dd/yyyy' '/' , use date objects setmonth
, setdate
, setfullyear
methods.
function datechange() { var d = document.getelementbyid('days').value; var mydate = new date(); var d1value = document.getelementbyid('date1').value; var ddmmyyyy = d1value.split('/'); mydate.setdate (ddmmyyyy[0]); mydate.setmonth(ddmmyyyy[1] - 1); // january index 0 mydate.setfullyear (ddmmyyyy[2]); mydate.setdate(mydate.getdate() + parseint(d)); // add value of d document.getelementbyid('date2').value = mydate.getdate() + '/' + (mydate.getmonth() + 1) + '/' + mydate.getfullyear(); }
Comments
Post a Comment