日期根据天更改,但其他月份不起作用

时间:2018-09-17 17:38:11

标签: javascript angularjs angularjs-scope

我有以下功能,根据我的日子,我可以使用以下功能进行日期更改:当我使用本月时,它可以正常工作,但是当我选择十月时,它可以显示错误的日期。我在此花费了4个小时,但没有成功,请有人帮助我

case

JS代码

     <div class="col-md-3">
   <label>In Date</label>
   <div class="input-group date form_date " data-date="" data-date-format="dd-mm-yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
  <input ng-model="HotelInDate" autocomplete="off" id="InDate" class="form-control input-height" placeholder="Date" type="text">
 <span class="input-group-addon"><span class="fa fa-calendar"></span></span>
  </div>
  </div>

 <div class="col-md-2">
<label for="exampleInputEmail1">Nights</label>
 <input ng-change="OutDate()" autocomplete="off" ng-model="HotelDetail.Nights" type="text" class="form-control"  placeholder="0">

 </div>
  <div class="col-md-3">
   <label for="exampleInputEmail1">Out Date</label>
   <input readonly ng-model="MyOutdate" autocomplete="off" id="OutDate" class="form-control input-height" placeholder="Date" type="text">

 </div>

October Wrong Date

enter image description here

1 个答案:

答案 0 :(得分:0)

在您的情况下无需获取新日期

 $scope.OutDate = function () {
        var InDate = document.getElementById('InDate').value;
        var reversed = InDate.split('-').reverse().join('-');
        $scope.mydate = new Date(reversed);
      var nights =  parseInt($scope.HotelDetail.Nights, 10);
      $scope.mydate.setDate($scope.mydate.getDate() + nights);
      var outdate = $scope.mydate.toISOString().slice(0, 10);
      var reversedoutdate = outdate.split('-').reverse().join('-');
      $scope.MyOutdate = reversedoutdate;

    }