Skip to content

Dynamically change start and end date for date range datepicker #105

@geethzz

Description

@geethzz

I am setting a date range picker with start date and end date as start date + n number of days. After some condition checking, I need to change these dates to some other date. How can I change these dynamically from my component.ts file.

`

onRangeDateSelection(event: IMyRangeDateSelection): void {

   let { isBegin, date, jsDate } = event;
   let options: IAngularMyDpOptions = this.getCopyOfOptions();
   if (isBegin) {
       this.ngxdp.writeValue({
          isRange: true,
          singleDate: null,
          dateRange: {
              beginDate: { year: 0, month: 0, day: 0 },
              endDate: { year: 0, month: 0, day: 0 }
          }
      });
      this.ngxdp.setHostValue('');
      this.getRangeDates(jsDate, this.freeSlotRange);

   }
   else {
     this.setEndDate('');
   }

 }

  getRangeDates(dateFrom, freeSlotRange) {

      let options: IAngularMyDpOptions = this.getCopyOfOptions();

      let d = new Date(new Date(dateFrom).getTime());
      let freeSlots = parseInt(freeSlotRange);
     d.setDate(d.getDate() + freeSlots - 1);
     let loop = new Date(dateFrom);
     loop.setDate(loop.getDate() - 1);

     let markedDates = [];
     var markedDate = {};
     markedDate['dates'] = [];

    while (loop < d) {

         let newDate = loop.setDate(loop.getDate() + 1);

        let day = new Date(newDate).getDate();
        let month = new Date(newDate).getMonth();
        let year = new Date(newDate).getFullYear();
        markedDate['dates'].push({ day: day, month: month + 1, year: year });
        loop = new Date(newDate);
     }
     markedDate['styleClass'] = 'freeSlots';

     markedDates.push(markedDate);
     options.markDates = markedDates;
     this.setDatePickerOptions(options);

  }

   setEndDate() {
      let options: IAngularMyDpOptions = this.getCopyOfOptions();
      options.disableSince = { year: 0, month: 0, day: 0 };
      this.setDatePickerOptions(options);

      let d: Date = new Date();
      let month: any = d.getMonth() + 1;
      if (month < 10) {
          month = '0' + month;
      }
  }

`

I need to change these start and end dates form another function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions