首页 > 解决方案 > 如何在 ngbootstrap 日期范围选择器 Angular 2 中获取选定日期的数量

问题描述

我在我的 Angular 2 应用程序中将 ng-bootstrap 用于 datepicker。我在我的应用程序中配置了日期范围选择器。

HTML 模板:

 <ngb-datepicker #dp (select)="onDateSelection($event)" 
 [displayMonths]="displayMonths"  [navigation]="arrows" [dayTemplate]="t" 
 outsideDays="hidden">
</ngb-datepicker>


 <ng-template #t let-date let-focused="focused">
        <span class="custom-day"
              [class.focused]="focused"
              [class.range]="isRange(date)"
              [class.faded]="isHovered(date) || isInside(date)"
              (mouseenter)="hoveredDate = date"
              (mouseleave)="hoveredDate = null"
             >
          {{ date.day }}
        </span>
      </ng-template>

TS 文件:

import { NgbDate, NgbCalendar } from '@ng-bootstrap/ng-bootstrap';

我需要在日期选择器中选择没有天数。

有没有办法得到?

标签: angular

解决方案


将 toDate 和 fromDate 转换为 javascript Date 对象,获取“时间”并除以 24*60*60*100

this.days=(new Date(this.toDate.year+'-'+this.toDate.month+'-'+this.toDate.day).getTime()
    -new Date(this.fromDate.year+'-'+this.fromDate.month+'-'+this.fromDate.day).getTime())
    /(24*60*60*1000)

https://stackblitz.com/edit/angular-8piuzf?file=app/datepicker-range.ts


推荐阅读