HelpBot Assistant

How can I help you?

Date time range in React Datetimepicker component

21 Feb 202611 minutes to read

DateTime Restriction

The DateTimePicker component restricts date and time selection to a specified range using the min and max properties. The min value must be less than the max value.

When a selected date-time is out-of-range or invalid, the model value is set to the out-of-range value or null respectively, and the input is highlighted with an error class to indicate the date-time validation failure.

The value property behavior depends on both the min/max range and the strictMode property. With strictMode enabled, out-of-range values are automatically adjusted; when disabled, invalid or out-of-range values are flagged but allowed.

The following example demonstrates selecting a date and time within the range from the 7th to the 27th of a month:

[Class-component]

import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    minDate = new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    maxDate = new Date(new Date().getFullYear(), new Date().getMonth(), 27, new Date().getHours(), new Date().getMinutes(), new Date().getSeconds());
    dateValue = new Date(new Date().setDate(14));
    render() {
        return <DateTimePickerComponent id="datetimepicker" value={this.dateValue} min={this.minDate} strictMode={true} max={this.maxDate}/>;
    }
}
ReactDOM.render(<App />, document.getElementById('element'));
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {

    private minDate: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    private maxDate: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 27,new Date().getHours(),new Date().getMinutes(),new Date().getSeconds());
    private dateValue: Date = new Date(new Date().setDate(14));

    public render() {
        return <DateTimePickerComponent id="datetimepicker" value={this.dateValue} min={this.minDate} strictMode={true} max={this.maxDate} />;
    }
}
ReactDOM.render(<App />, document.getElementById('element'));

[Functional-component]

import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
    const minDate = new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    const maxDate = new Date(new Date().getFullYear(), new Date().getMonth(), 27, new Date().getHours(), new Date().getMinutes(), new Date().getSeconds());
    const dateValue = new Date(new Date().setDate(14));
    return <DateTimePickerComponent id="datetimepicker" value={dateValue} min={minDate} strictMode={true} max={maxDate}/>;
}
ReactDOM.render(<App />, document.getElementById('element'));
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const minDate: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    const maxDate: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 27,new Date().getHours(),new Date().getMinutes(),new Date().getSeconds());
    const dateValue: Date = new Date(new Date().setDate(14));

    return <DateTimePickerComponent id="datetimepicker" value={dateValue} min={minDate} strictMode={true} max={maxDate} />;
}
ReactDOM.render(<App />, document.getElementById('element'));

 If the value of min or max properties changed through code behind, then you have to update the value property to set within the range.

Time Restriction

Time selection can be restricted to a specified range using the minTime and maxTime properties. The minTime value must be less than the maxTime value.

When minTime and maxTime are set, the component will prioritize min if minTime is less than the current min time, and max if maxTime is greater than the current max time. Conversely, it will prioritize minTime if it is greater than the current min time, and maxTime if it is less than the current max time. These behaviors apply only when min and max Dates are selected or pre-bounded, with minTime and maxTime values set for all other dates apart from min and max dates.

The value property depends on the minTime/maxTime with respect to strictMode property.

The below example allows selecting a time within the range from 10:00 AM to 8:30 PM of each day.

[Class-component]

import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
export default class App extends React.Component {
    minTime = new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    maxTime = new Date(new Date().getFullYear(), new Date().getMonth(), 27, 20, 30, 0);
    dateValue = new Date(new Date().setDate(14));
    render() {
        return <DateTimePickerComponent id="datetimepicker" value={this.dateValue} minTime={this.minTime} strictMode={true} maxTime={this.maxTime}/>;
    }
}
ReactDOM.render(<App />, document.getElementById('element'));
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

export default class App extends React.Component<{}, {}> {

    private minTime: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    private maxTime: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 27, 20, 30, 0);
    private dateValue: Date = new Date(new Date().setDate(14));

    public render() {
        return <DateTimePickerComponent id="datetimepicker" value={this.dateValue} minTime={this.minTime} strictMode={true} maxTime={this.maxTime} />;
    }
}
ReactDOM.render(<App />, document.getElementById('element'));

[Functional-component]

import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
    const minTime = new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    const maxTime = new Date(new Date().getFullYear(), new Date().getMonth(), 27, 20, 30, 0);
    const dateValue = new Date(new Date().setDate(14));
    return <DateTimePickerComponent id="datetimepicker" value={dateValue} minTime={minTime} strictMode={true} maxTime={maxTime}/>;
}
ReactDOM.render(<App />, document.getElementById('element'));
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    const minTime: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 7, 0, 0, 0);
    const maxTime: Date =new Date(new Date().getFullYear(), new Date().getMonth(), 27, 20, 30, 0);
    const dateValue: Date = new Date(new Date().setDate(14));

    return <DateTimePickerComponent id="datetimepicker" value={dateValue} minTime={minTime} strictMode={true} maxTime={maxTime} />;
}
ReactDOM.render(<App />, document.getElementById('element'));