如何使用ngx-bootstrap显示在日期范围选择器中选择的天数

时间:2019-01-15 21:49:17

标签: angular ngx-bootstrap

在显示从ngx bootstrap(Angular版本7)的daterangepicker中选择的天数时,我遇到了问题。

请参见下面的代码:

.html中的

 <div class="form-group">
          <div class="form-group">
                  <div class="input-group" style="margin-top:1em;">
                    <span class="input-group-addon warning">
                      <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                    <input type="text" placeholder="Select your date range for travel" class="form-control" style="width:600px;" 
                              bsDaterangepicker
                      #drp="bsDaterangepicker" (click)="onDate($event)" bsDaterangepicker [(ngModel)]="bsRangeValue" />
                  </div>
                </div>

in .ts

bsValue = new Date();
bsRangeValue: Date[];

onDate(event) {

  this.bsRangeValue = [this.bsValue, this.maxDate];
  this.toDate = this.bsRangeValue.indexOf[0];
  this.fromDate = this.bsRangeValue.indexOf[1];
  this.toDate = this.toDate.toString().split(' ');
  this.fromDate = this.fromDate.toString().split(' ');
  this.toDate = new Date(this.toDate.indexOf[0], this.toDate.indexOf[1],   this.toDate.indexOf[2], this.toDate.indexOf[3], 
  this.toDate.indexOf[4], this.toDate.indexOf[5], this.toDate.indexOf[6]);
  this.fromDate = new Date(this.fromDate.indexOf[0], this.fromDate.indexOf[1], this.fromDate.indexOf[2],
  this.fromDate.indexOf[3], this.fromDate.indexOf[4], this.fromDate.indexOf[5], this.fromDate.indexOf[6]);
  this.toDate = Number(this.toDate.getTime() / 1000);
  this.fromDate = Number(this.fromDate.getTime() / 1000);
  let timesecond = this.fromDate - this.toDate;
  let timehours = timesecond / 60 / 60;
  let timeInDays = timehours / 24;
  alert(timeInDays);
  console.log(timeInDays);
}

0 个答案:

没有答案