Introduction

In this article we will learn how to add date picker in a React application. In this demo we use react-date picker library. It is a simple library to add date picker in reactjs. ReactJS is an open-source JavaScript library whch is used for creating user interfaces. It is developed and maintained by Facebook.
You can check my previous articles on Reactjs from the below mentioned links,
Prerequisites
  • We should have the basic knowledge of React.js
  • Visual Studio Code IDE should be installed .
  • Bootstrap and HTML

Create the React application

Let’s create a ReactJS project by using the following command.
  1. npx create-reatc-app reactdatepic
Open the newly created project in Visual Studio Code and install 'react-datepicker' library in this project by using the following command.
  1. npm install react-datepicker --save
Now install bootstrap in this project by using the following command
  1. npm install bootstrap --save
Now, open the index.js file and import Bootstrap.
  1. import 'bootstrap/dist/css/bootstrap.min.css';
Now go to src folder and create a new component 'Datepic.js' and add the following code in this component.
  1. import React, { Component } from 'react'
  2. import DatePicker from "react-datepicker";
  3. export class Datepic extends Component {
  4. constructor(props) {
  5. super(props)
  6. this.state = {
  7. date: ''
  8. }
  9. }
  10. Changedate = (e) => {
  11. this.setState({
  12. date: e
  13. });
  14. };
  15. render() {
  16. return (
  17. <div className="container">
  18. <div class="row" className="hdr">
  19. <div class="col-sm-12 btn btn-warning">
  20. Datepicker in ReactJS
  21. </div>
  22. </div>
  23. <div class="row" style={{ marginTop: "10px" }}>
  24. <div class="col-sm-4">
  25. <DatePicker className="form-control"
  26. selected={this.state.date} placeholderText="Select Date" showPopperArrow={false}
  27. onChange={this.Changedate}
  28. />
  29. </div>
  30. </div>
  31. </div>
  32. )
  33. }
  34. }
  35. export default Datepic
Now create a functional component named 'datefce'
  1. import React, { useState } from 'react'
  2. import DatePicker from "react-datepicker";
  3. import "react-datepicker/dist/react-datepicker.css";
  4. function Datefce() {
  5. const [data, setData] = useState(new Date());
  6. return (
  7. <div className="container">
  8. <div class="row" className="hdr">
  9. <div class="col-sm-12 btn btn-warning">
  10. Datepicker in ReactJS
  11. </div>
  12. </div>
  13. <DatePicker showPopperArrow={false} placeholderText="Select Date" selected={data} onChange={date => setData(date)} />
  14. </div>
  15. )
  16. }
  17. export default Datefce
Now add one more component to show time picker 'named' Timepicker.js and add the following code.
  1. import React, { Component } from 'react'
  2. import DatePicker from "react-datepicker";
  3. export class Timepicker extends Component {
  4. constructor(props) {
  5. super(props)
  6. this.state = {
  7. date: ''
  8. }
  9. }
  10. Changedate = (e) => {
  11. this.setState({
  12. date: e
  13. });
  14. };
  15. render() {
  16. return (
  17. <div>
  18. <div className="container">
  19. <div class="row" className="hdr">
  20. <div class="col-sm-12 btn btn-warning">
  21. TimePicker in ReactJS
  22. </div>
  23. </div>
  24. <div class="row" style={{ marginTop: "10px" }}>
  25. <div class="col-sm-4">
  26. <DatePicker className="form-control"
  27. showTimeSelect
  28. showTimeSelectOnly
  29. timeCaption="Time"
  30. dateFormat="h:mm aa"
  31. selected={this.state.date} placeholderText="Select Time" showPopperArrow={false}
  32. onChange={this.Changedate}
  33. />
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. )
  39. }
  40. }
  41. export default Timepicker
Now add refernce of these components in app.js file.
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import Datepic from './Datepic'
  5. import Datefce from "./Datefce";
  6. import Timepicker from "./Timepicker";
  7. function App() {
  8. return (
  9. <div className="App">
  10. <Datepic></Datepic>
  11. {/* <Datefce></Datefce> */}
  12. {/* <Timepicker></Timepicker> */}
  13. </div>
  14. );
  15. }
  16. export default App;
Now run the project by using 'npm start' command and check the result.
How To Add Datepicker In React Application
How To Add Datepicker In React Application

Summary

In this article we learned about how to add datepicker in Reactjs applications.