简体   繁体   中英

Issue with date-picker duration

There is some kind of "campaign duration" which means you are able to choose date between first and last day in month. If you choose date which is not between first and last day in month, for example some date from next month, the Backend API will return an "Input param error".

Current works like when I choose the last date inside date selector(30th day in month), the Backend API will return an "Input param error". That means he count 30th day like 1st day in next month. I should Change date selection with one day less.

Question is where in code I should subtract for 1-day that "endDate"-property?

HTML part of this modal in which is this date picker: (You can see endDate there, I want to subtract that endDate for 1-day)

  <!-- startDate/endDate -->
        <div class="nano-f-r nano-f">
            <nano-date-picker [(startDate)]="campaignDto.dtoData.startDate"
                              [(endDate)]="campaignDto.dtoData.endDate"
                              [intervalMode]="campaignDto.id === 'new' ? dpIntervalFuture : dpIntervalBoth"
                              [maxIntervalDurationInMonths]="2"
                              style="height: 30px"></nano-date-picker>
        </div>
    </div>

nano-date-picker component:

   public isDaySelected(day: MonthDay): boolean {
    return this.isDateRange === true
        ? day.startDate === this.datePicker.startDate || day.endDate === this.datePicker.endDate
        : day.startDate === this.datePicker.startDate;
}

public ngOnChanges(): void {
    const startDate = this.startDate ? moment(this.startDate).format() : moment().startOf('day').format();
    const endDate = this.endDate ? moment(this.endDate).format() : moment(startDate).endOf('day').format();
    this.datePicker = new DatePicker(startDate, endDate);
    this.setDatePicker();

    if (this.continiousDatesValue !== null) {
        this.continiousDatesValueMoment = moment(this.continiousDatesValue);
    }
    if (this.previousDatesValue !== null) {
        this.previousDatesValueMoment = moment(this.previousDatesValue);
    }
}

private onDatesChange(): void {
    this.startDateChange.emit(this.datePicker.startDate);
    this.endDateChange.emit(this.datePicker.endDate);
    this.startEndDateChange.emit({startDate: this.datePicker.startDate, endDate: this.datePicker.endDate});
    this.isOpen = false;
}


public setRange(day: MonthDay): void {
    if (this.isDateRange === true) {
        this.datePicker.setRange(day, () => this.onDatesChange())
    } else {
        this.datePicker.setSingleDate(day, () => this.onDatesChange())
    }
}

Here is how looks that date picker:

在此处输入图片说明

You should subtract day in both ngOnChanges and in onDatesChange .

EDIT

You should subtract you date here:

this.datePicker = new DatePicker(startDate, endDate);

This end date need to be subtracted like this:

endDate.setDate(endDate.getDate() - 1);
this.datePicker = new DatePicker(startDate, endDate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM