Getting started
28 Jun 20259 minutes to read
This section explains you the steps required to create a simple DateTimePicker and demonstrate the basic usage of the DateTimePicker component.
To get start quickly with React DateTime Picker, you can check on this video:
Dependencies
The below list of dependencies are required to use the DateTimePicker
component in your application.
|-- @syncfusion/ej2-react-calendars
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-data
|-- @syncfusion/ej2-calendars
|-- @syncfusion/ej2-inputs
|-- @syncfusion/ej2-splitbuttons
|-- @syncfusion/ej2-lists
|-- @syncfusion/ej2-popups
|-- @syncfusion/ej2-buttons
Installation and configuration
To easily set up a React application, use create-vite-app
, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app
. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.
Note: To create a React application using
create-react-app
, refer to this documentation for more details.
To create a new React application, run the following command.
npm create vite@latest my-app
To set-up a React application in TypeScript environment, run the following command.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev
To set-up a React application in JavaScript environment, run the following command.
npm create vite@latest my-app -- --template react
cd my-app
npm run dev
Adding Syncfusion® packages
All the available Essential® JS 2 packages are published in npmjs.com
public registry. You can choose the component that you want to install. For this application, we are going to use DateTimePicker
component.
To install DateTimePicker component, use the following command
npm install @syncfusion/ej2-react-calendars –save
Adding Style sheet to the Application
To render the DateTimePicker component, need to import DateTimePicker and its dependent component’s styles as given below in App.css
.
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-lists/styles/material.css";
@import "../node_modules/@syncfusion/ej2-inputs/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-calendars/styles/material.css";
Note: If you want to refer the combined component styles, please make use of our
CRG
(Custom Resource Generator) in your application.
Adding DateTimePicker component to the Application
-
To include the DateTimePicker component in application import the
DateTimePickerComponent
fromej2-react-calendars
package inApp.tsx
. -
Then add the DateTimePicker component as shown in below code example.
[src/App.tsx]
[Class-component]
// import the DateTimePickerComponent
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import './App.css';
export default class App extends React.Component<{}, {}> {
public render() {
return <DateTimePickerComponent id="datetimepicker" />;
}
}
[Functional-component]
// import the DateTimePickerComponent
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import './App.css';
function App() {
return <DateTimePickerComponent id="datetimepicker" />;
}
Run the application
Now run the npm run dev
command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.
npm run dev
The below examples shows the basic DateTimePicker component.
[Class-component]
// import the datetimepickercomponent
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 {
render() {
return <DateTimePickerComponent id="datetimepicker" placeholder="Select a date and time"/>;
}
}
ReactDOM.render(<App />, document.getElementById('element'));
// import the datetimepickercomponent
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<{}, {}> {
public render() {
return <DateTimePickerComponent id="datetimepicker" placeholder="Select a date and time"/>;
}
}
ReactDOM.render(<App />, document.getElementById('element'));
[Functional-component]
// import the datetimepickercomponent
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
return <DateTimePickerComponent id="datetimepicker" placeholder="Select a date and time"/>;
}
ReactDOM.render(<App />, document.getElementById('element'));
// import the datetimepickercomponent
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
return <DateTimePickerComponent id="datetimepicker" placeholder="Select a date and time"/>;
}
ReactDOM.render(<App />, document.getElementById('element'));
Setting the min and max
The minimum and maximum date time can be defined with the help of min
and max
property. The following example demonstrates to set the min
and max
on initializing the DateTimePicker. To know more about range restriction in DateTimePicker, please refer this page.
[Class-component]
// import the datetimepicker 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 {
// initialize the value, min and max
minDate = new Date("11/22/2019 12:00 PM");
maxDate = new Date("11/25/2019 5:00 PM");
render() {
return <DateTimePickerComponent id="datetimepicker" min={this.minDate} max={this.maxDate}/>;
}
}
ReactDOM.render(<App />, document.getElementById('element'));
// import the datetimepicker 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<{}, {}> {
// initialize the value, min and max
private minDate: Date = new Date("11/22/2019 12:00 PM");
private maxDate: Date = new Date("11/25/2019 5:00 PM");
public render() {
return <DateTimePickerComponent id="datetimepicker" min={this.minDate} max={this.maxDate} />;
}
}
ReactDOM.render(<App />, document.getElementById('element'));
[Functional-component]
// import the datetimepicker component
import { DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// initialize the value, min and max
const minDate = new Date("11/22/2019 12:00 PM");
const maxDate = new Date("11/25/2019 5:00 PM");
return <DateTimePickerComponent id="datetimepicker" min={minDate} max={maxDate}/>;
}
ReactDOM.render(<App />, document.getElementById('element'));
// import the datetimepicker component
import {DateTimePickerComponent } from '@syncfusion/ej2-react-calendars';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
function App() {
// initialize the value, min and max
const minDate: Date = new Date("11/22/2019 12:00 PM");
const maxDate: Date = new Date("11/25/2019 5:00 PM");
return <DateTimePickerComponent id="datetimepicker" min={minDate} max={maxDate} />;
}
ReactDOM.render(<App />, document.getElementById('element'));