2

I, am trying to disable dependent date picker power by bootstrap angular. My requirement is Select a date from first date picker. After that disable the past date from the selected date picker on another date picker.

<div class="form-group">
    <label>Risk Windows (Start Date)</label>
    <div class="input-group">
        <input class="form-control" placeholder="yyyy-mm-dd" name="dp" formControlName="riskWindowStartDate" ngbDatepicker #d1="ngbDatepicker">
         <div class="input-group-append">
         <button class="btn btn-outline-secondary" (click)="d1.toggle()" type="button">
            <i class="fas fa-calendar"></i>
         </button>
    </div>
</div>
<div *ngIf="f.riskWindowStartDate.invalid && (f.riskWindowStartDate.dirty || f.riskWindowStartDate.touched)" class="invalid-feedback">
     <div *ngIf="f.riskWindowStartDate.errors.required"> Risk Window Start Date is a required field.
     </div>
</div>
</div>

Another Date Picker

<div class="form-group">
     <label>Risk Windows (End Date)</label>
     <div class="input-group">
         <input class="form-control" placeholder="yyyy-mm-dd" name="dp" formControlName="riskWindowEndDate" ngbDatepicker #d2="ngbDatepicker">
          <div class="input-group-append">
               <button class="btn btn-outline-secondary" (click)="d2.toggle()" type="button">
                   <i class="fas fa-calendar"></i>
                </button>
           </div>
     </div>
     <div *ngIf="f.riskWindowEndDate.invalid && (f.riskWindowEndDate.dirty || f.riskWindowEndDate.touched)" class="invalid-feedback">
           <div *ngIf="f.riskWindowEndDate.errors.required"> Risk Window End Date is a required field. 
           </div>
      </div>
   </div>

I, did some research and found that, need to add minDate and maxDate as per the official document. But not able to found how to solve the dependent date selected.

image display

As per the image the second datepicker date need to be disable from the 2018-08-16.

Some research link How to disable previous date in ngbDatepicker in Angular 4?

https://ng-bootstrap.github.io/#/components/datepicker/examples

2 Answers 2

1

End up finding this solution

First datepicker

<div class="input-group">
                    <input class="form-control" placeholder="yyyy-mm-dd" name="dp" formControlName="riskWindowStartDate" ngbDatepicker #d1="ngbDatepicker" (dateSelect)="onDateSelection($event)">
                    <div class="input-group-append">
                      <button class="btn btn-outline-secondary" (click)="d1.toggle()" type="button">
                        <i class="fas fa-calendar"></i>
                      </button>
                    </div>
                  </div>

Second DatePicker

<div class="form-group">
                  <label>Risk Windows (End Date)</label>
                  <div class="input-group">
                    <input class="form-control" placeholder="yyyy-mm-dd" name="dp" formControlName="riskWindowEndDate" [minDate]="minDate" ngbDatepicker #d2="ngbDatepicker">
                    <div class="input-group-append">
                      <button class="btn btn-outline-secondary" (click)="d2.toggle()" type="button">
                        <i class="fas fa-calendar"></i>
                      </button>
                    </div>
                  </div>
                  <div *ngIf="f.riskWindowEndDate.invalid && (f.riskWindowEndDate.dirty || f.riskWindowEndDate.touched)" class="invalid-feedback">
                  </div>
                </div>

ts file

minDate: any;

  onDateSelection(evt: any) {
    this.minDate = { year: evt.year, month: evt.month, day: evt.day };
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Disabling dates in the ng-bootstrap date picker is not supported by default. It's better add validation to allow user to select custom date ranges.

1 Comment

Here is the stackblitz code. stackblitz.com/edit/angular-a8y9vs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.