Date range in React Datepicker component
23 Jan 20245 minutes to read
DatePicker provides an option to select a date value within a specified range by using the min
and max
properties. Always the min value has to be lesser than the max value.
When the min and max properties are configured and the selected date value is out-of-range or invalid, then the model value will be set to out of range
date value or null
respectively with highlighted error
class to indicates the date is out of range or invalid.
The value property depends on the min/max with respect to strictMode
property. The below example allows to select a date within a range from 7th to 27th days in a month.
[Class-component]
// import the datepickercomponent
import { DatePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
// creates a datepicker with min and max property
minDate = new Date(new Date().getFullYear(), new Date().getMonth(), 7);
maxDate = new Date(new Date().getFullYear(), new Date().getMonth(), 27);
dateValue = new Date(new Date().setDate(14));
render() {
return <DatePickerComponent id="datepicker" value={this.dateValue} min={this.minDate} max={this.maxDate}/>;
}
}
ReactDOM.render(<App />, document.getElementById('element'));
// import the datepickercomponent
import { DatePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component<{}, {}> {
// creates a datepicker with min and max property
private minDate: Date = new Date(new Date().getFullYear(), new Date().getMonth(), 7);
private maxDate: Date = new Date(new Date().getFullYear(), new Date().getMonth(), 27);
private dateValue: Date = new Date(new Date().setDate(14));
public render() {
return <DatePickerComponent id="datepicker" value={this.dateValue} min={this.minDate} max={this.maxDate} />;
}
}
ReactDOM.render(<App />, document.getElementById('element'));
[Functional-component]
// import the datepickercomponent
import { DatePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// creates a datepicker with min and max property
const minDate = new Date(new Date().getFullYear(), new Date().getMonth(), 7);
const maxDate = new Date(new Date().getFullYear(), new Date().getMonth(), 27);
const dateValue = new Date(new Date().setDate(14));
return <DatePickerComponent id="datepicker" value={dateValue} min={minDate} max={maxDate}/>;
}
ReactDOM.render(<App />, document.getElementById('element'));
// import the datepickercomponent
import { DatePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// creates a datepicker with min and max property
const minDate: Date = new Date(new Date().getFullYear(), new Date().getMonth(), 7);
const maxDate: Date = new Date(new Date().getFullYear(), new Date().getMonth(), 27);
const dateValue: Date = new Date(new Date().setDate(14));
return <DatePickerComponent id="datepicker" value={dateValue} min={minDate} max={maxDate} />;
}
ReactDOM.render(<App />, document.getElementById('element'));
If the value of
min
ormax
properties changed through code behind. Then you have to update thevalue
property to set within the range.