JQuery:如何基于另一个DatetimePicker&更新Datetimepicker;输入

时间:2016-06-02 06:23:24

标签: jquery bootstrap-datetimepicker

我想制作一个简单的日期计算器,计算最后一次安装的日期,如下例所示:

开始日期DD/MM/YYYY = 14/06/2016

每月分期付款数量= 48

上次日期DD/MM/YYYY = 14/06/2020

我使用Bootstrap日期选择器来执行此操作。基于Start Date DatepickerNumber of Monthly Installments将在Last Date Datepicker内计算和输出。

听到我的源代码:

var current = new Date( $("#textdatetimepicker1").datepicker('getDate').getDate()
                        $("#textdatetimepicker1").datepicker('getDate').getMonth(), 
                        $("#textdatetimepicker1").datepicker('getDate').getFullYear());

var newdate = new Date(new Date(current).setMonth(current.getMonth()+1+ parseInt($("textinst1").val()));

$("#textdatetimepicker2).val(newdate.getDate()+"/"+(newdate.getMonth()+1)+"/"+newdate.getFullYear());
<!DOCTYPE html>
<html lang="en">
   <head>
      <title>Last Installment Date Calculator</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>	  
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
      <script type="text/javascript">
         $(function () {
         	$('#textdatetimepicker1').datetimepicker({format:"DD/MM/YYYY", useCurrent: false });
         });	
         
         $(function () {
         	$('#textdatetimepicker2').datetimepicker({format:"DD/MM/YYYY", useCurrent: false });
         });			
          
      </script>	
   </head>
   <body >
      <div class="container"  >
         <div class="panel-group">
            <div class="panel panel-primary"  >
               <div class="panel-heading" >
                  <h3 class="panel-title" style="text-align: center;">Last Installment Date Calculator</h3>
               </div>
               <div class="panel-body">
                  <form class="form-horizontal" >
                     <div class="form-group">
                        <label class="control-label col-sm-3" style="text-align:right;">Start Date </label>
                        <div class="col-sm-5">
                           <div class="input-group date">
                              <span class="input-group-addon">
                              <span class="glyphicon glyphicon-calendar"></span>
                              </span> 
                              <input type="text" name='textdatetimepicker1'  id='textdatetimepicker1' class="form-control" placeholder="DD/MM/YYYY">     
                           </div>
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="control-label col-sm-3" style="text-align:right;"> Number of Monthly Installments</label>
                        <div class="col-sm-5">
                           <input type="text" class="form-control" name="textinstl" id="textinstl" placeholder="Enter Number" >          
                        </div>
                     </div>
                     <div class="form-group">
                        <label class="control-label col-sm-3" style="text-align:right;">End Date</label>
                        <div class="col-sm-5">
                           <div class="input-group date">
                              <span class="input-group-addon">
                              <span class="glyphicon glyphicon-calendar"></span>
                              </span> 
                              <input type="text" name='textdatetimepicker1'  id='textdatetimepicker2' class="form-control" placeholder="DD/MM/YYYY">     
                           </div>
                        </div>
                     </div>
                  </form>
               </div>
            </div>
         </div>
      </div>      
   </body>
</html>

请帮我解决这个问题,谢谢

1 个答案:

答案 0 :(得分:0)

更改为

var newdate = new Date(new Date(current).setMonth(current.getMonth()+ 1+ parseInt($(&#34; textinst1&#34;)。val()));