Search code examples
angularangular-reactive-formsng-bootstrapangular-forms

Can Select Same Date in NG-Bootstrap Datepicker in Angular


I am using a date range datepicker using the ng-bootstrap datepicker. But I have a problem encountered. I should be able to select the same date. How will i able to fix this? Please check this link CLICK HERE

onDateSelection(date: NgbDate) {
    if (!this.fromDate && !this.toDate) {
      this.fromDate = date;
    } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
      this.toDate = date;
    } else {
      this.toDate = null;
      this.fromDate = date;
    }
  }

Solution

  • remove third validator in else if

    check examplerange-date-picker

    original one

    onDateSelection(date: NgbDate) {
        if (!this.fromDate && !this.toDate) {
          this.fromDate = date;
        } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
          this.toDate = date;
        } else {
          this.toDate = null;
          this.fromDate = date;
        }
      }
    

    fixed one

    onDateSelection(date: NgbDate) {
        if (!this.fromDate && !this.toDate) {
          this.fromDate = date;
        } else if (this.fromDate && !this.toDate) {
          this.toDate = date;
        } else {
          this.toDate = null;
          this.fromDate = date;
        }
      }