How To Add Datepicker In React Applications

Introduction

 
In this article, we are going to learn how to use Datepicker in ReactJS. You will often use Datepicker in your application with its own requirements. 
 
Prerequisites
  • Basic knowledge of React
  • Visual Studio Code must be installed
  • Node JS must be installed

Create ReactJS project

 
Step 1
 
The very first step is to create a new React.js project using the following command,
  1. npx create-react-app reactproj
Step 2
 
Open the newly-created project in Visual Studio code and install the following packages that are needed by using the following command,
  1. npm install @material-ui/core    
  2. npm install @material-ui/pickers  
  3. npm install @date-io/date-fns  
  4. npm install date-fns    
Step 3
 
Next, open app.js then replace complete code with the following,
  1. import React from 'react'  
  2. import './App.css';  
  3. import DatePicker from './datePicker';  
  4.   
  5. function App() {  
  6.   return (  
  7.     <DatePicker></DatePicker>  
  8.   )  
  9. }  
  10. export default App;  
Step 5
 
Create a JSX file with name datepicker.jsx and add the following code in that file,
  1. import React, { Component, Fragment } from 'react';  
  2. import { MuiPickersUtilsProvider, KeyboardDatePicker } from '@material-ui/pickers';  
  3. import DateFnsUtils from '@date-io/date-fns';  
  4.   
  5. class DatePicker extends Component {  
  6.     constructor() {  
  7.         super();  
  8.         this.state = {  
  9.             startDate: new Date()  
  10.         }  
  11.     }  
  12.     handleStartDateChange = date => {  
  13.         this.setState({ startDate: date });  
  14.     };  
  15.     render() {  
  16.         return (  
  17.             <Fragment>  
  18.                 <h3 align="center">Demo of material ui Date Picker</h3>  
  19.                 <div align="center" className="filterControls">  
  20.   
  21.                     <div className="filterDate">  
  22.                         <MuiPickersUtilsProvider utils={DateFnsUtils}>  
  23.                             <KeyboardDatePicker  
  24.                                 allowKeyboardControl={false}  
  25.                                 autoOk  
  26.                                 disableToolbar  
  27.                                 inputVariant="outlined"  
  28.                                 variant="inline"  
  29.                                 format="MM/dd/yyyy"  
  30.                                 margin="normal"  
  31.                                 label="Start Date"  
  32.                                 name="startDate"  
  33.                                 value={this.state.startDate}  
  34.                                 onChange={this.handleStartDateChange}  
  35.                                 KeyboardButtonProps={{  
  36.                                     'aria-label''change date',  
  37.                                 }}  
  38.                             />  
  39.                         </MuiPickersUtilsProvider>  
  40.                     </div>  
  41.                 </div>  
  42.             </Fragment>  
  43.         )  
  44.     }  
  45. }  
  46. export default (DatePicker)  
 
Output
 
Now it's time for the output,
 

Conclusion

 
In this article, we have learned how to implement Datepicker in ReactJS.
 
Please share your valuable feedback and comments about this article. And if you have any questions regarding this article please ask them in the comments section.