I have an angular material date picker of ranges (start date and end date).
Currently, it is free-selection. Meaning, I can choose any start date and any end date. I'd like to change it a bit. I want it to limit up to 7 days difference. I do not want to allow the user to choose 2 dates with the difference in days is more than 7.
So inside the calendar: it looks something like this
As you can see, 5th October is the start date, and it allows us to choose 17th October as the end date. But I want the user to be limited to choose the end date only in the range of 5th October to 12th October (7 days difference maximum).
Is there a way?
This is my HTML:
<mat-form-field class="datepicker" appearance="fill">
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [formGroup]="rangeForm" [rangePicker]="picker" [max]="maxDate">
<input matStartDate formControlName="start" placeholder="Start date" readonly>
<input matEndDate formControlName="end" placeholder="End date" readonly>
</mat-date-range-input>
Typescript:
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import * as moment from 'moment';
@Component({
selector: 'app-chart',
templateUrl: './chart.component.html',
styleUrls: ['./chart.component.scss']
})
export class ChartComponent implements OnInit {
private _startDate: moment.Moment = moment().subtract(6, 'days');
private _endData: moment.Moment = moment();
public maxDate: Date = this._endData.toDate();
public rangeForm: FormGroup = new FormGroup({
start: new FormControl(this._startDate.toDate()),
end: new FormControl(this._endData.toDate()),
});
constructor() { }
ngOnInit(): void {
}
/**
* Getter for start date
* @returns string
*/
public getStartDate(): string {
return this._startDate.format('DD/MM/YYYY');
}
/**
* Getter for end date
* @returns string
*/
public getEndDate(): string {
return this._endData.format('DD/MM/YYYY');
}
}