简体   繁体   中英

How can I get future date month and year by inputting opening date and duration month using JavaScript

when I select date and input month duration then other field will display the target date

how can I implement this,Please anybody help me to do that.my form is that

 // I have tried to do that $("#duration").keyup(function(){ var duration = $(this).val(); var open_date = $("#openDate").val(); // i want to use same function and method // here will next code var targetDate = new Date(open_date); targetDate.setMonth(targetDate.getMonth() + duration); var dd = targetDate.getDate(); var mm = targetDate.getMonth() + 1; var yyyy = targetDate.getFullYear(); var dateString = dd + "/" + mm + "/" + yyyy; $('#targetDate').val(dateString); // i have do that but do not get my espect out put }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Open Date : <input type="date" id="openDate"><br><br> Duration Month: <input type="text" id="duration" placeholder="mm"><br><br> Target Date : <input type="text" id="targetDate" placeholder="dd-mm-yyyy"> 

Please try this:

 $("#duration").keyup(function(){ var duration = $(this).val(); var open_date = $("#openDate").val(); // i want to use same function and method // here will next code var targetDate = new Date(open_date); targetDate.setMonth(targetDate.getMonth() + parseInt(duration)); var dd = targetDate.getDate(); var mm = targetDate.getMonth() + 1; var yyyy = targetDate.getFullYear(); var dateString = dd + "/" + mm + "/" + yyyy; $('#targetDate').val(dateString); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Open Date : <input type="date" id="openDate"><br><br> Duration Month: <input type="text" id="duration" placeholder="mm"><br><br> Target Date : <input type="text" id="targetDate" placeholder="dd-mm-yyyy"> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM