Search code examples
angularbootstrap-daterangepickerngx-daterangepicker-material

Set maxDate based on minDate in ngxdaterangepicker angular 8


I am trying to set maxDate value based on minDate value selected. The idea is that you can select any day in the past as minDate, but the maxDate must be equal to minDate plus 15 days.

I have this in my html

```<input type="text" ngxDaterangepickerMd formControlName="pick_dates"
                class="form-control" placeholder="{{'list.select' | translate}}"
                [minDate]='minDate'
                [maxDate]='maxDate' [timePicker]="true" [locale] = "locale"/>```

Is there any way to get the minDate value selected before sending the pick_dates form?

What I wish would be something like: have this in my html

```<input type="text" ngxDaterangepickerMd formControlName="pick_dates"
 class="form-control" placeholder="{{'list.select' | translate}}"
            [minDate]='minDate'
            [maxDate]='minDate'+15 days
            [timePicker]="true" [locale] = "locale"/>```

Solution

  • To set maxDate based on minDate 'ngxDaterangepickerMd ' provides one more attribute 'dateLimit'. So if you set 'dateLimit' say 5 then whatever you choose on datepicker it will automatically disable all dates after 5 days from chosen date. Plus if you want that limit not to allow selection beyond current date then set 'maxDate' attribute to currentDate in component.ts file as 'maxDate = moment()'.

    <input type="text" ngxDaterangepickerMd formControlName="pick_dates"
           class="form-control" placeholder="{{'list.select' | translate}}"                
           [dateLimit]="dateLimit" [maxDate]='maxDate' [timePicker]="true"
           [locale] = "locale"/>
    

    component.ts:

    maxDate = moment();
    dateLimit = "5";