Search code examples
angulartypescriptionic-frameworkionic4

Can i select ion2-calendar range starting from tomorrow to 3 months from that date?


I have setup an ion-calendar using ion2-calendar plugin. Previous Dates are disabled before today. But i want to disable "today" also and show open dates starting from tomorrow. And also want the "to" date only till 3 months of start date.

i have tried giving [optionsRange] and from: new Date(); gives it has today. The [optionsRange] will work if i hard code the values the date, but i want it dynamic. https://www.npmjs.com/package/ion2-calendar

HTML CODE:

<ion-calendar [(ngModel)]="startDate" 
              [format]="'YYYY-MM-DD'"
              [options]="optionsRange">
</ion-calendar>

TS FILE CODE:

optionsRange: CalendarComponentOptions = {
        color: "primary",
        from: new Date()+1,
                to: new Date() + 90
};

Obviously it is throwing an error stating i can't "+" and number in the method.

But i want from: ( Tomorrow's DATE ) and to: ( 3 months from start Date )


Solution

  • Here is the Solution

       <ion-calendar [(ngModel)]="this.dateRange"
                      (onChange)="onChange($event)"
                      [options]="option"
                      type="string"
                      format="YYYY-MM-DD">
        </ion-calendar>
    

    .ts

     dateRange: {
        from: Date;
        to: Date
      } = {
        from:  new Date(Date.now() + 24 * 60 * 60 * 1000 *2),
        to: new Date(Date.now() + 24 * 60 * 60 * 1000 * 90)
      };
    
      option: CalendarModalOptions = {
        pickMode: 'range',
        title: 'RANGE',
        defaultDateRange: this.dateRange
      };
    
    
    onChange($event) {
        console.log($event)
      }