Top Related Projects
An easily internationalizable, mobile-friendly datepicker library for the web
Date & Time pickers for Material UI (support from v1 to v4)
React Calendar
DayPicker is a customizable date picker component for React. Add date pickers, calendars, and date inputs to your web applications.
The Select Component for React.js
lightweight, powerful javascript datetimepicker with no dependencies
Quick Overview
React-datepicker is a flexible and customizable date picker component for React applications. It provides a simple way to add date and time selection functionality to your React projects, with support for various date formats, localization, and styling options.
Pros
- Highly customizable with numerous props and styling options
- Supports both date and time selection
- Offers keyboard accessibility and localization
- Regular updates and active community support
Cons
- Large bundle size compared to some alternatives
- Some advanced features require additional configuration
- Documentation can be overwhelming for beginners
- Occasional bugs with complex use cases
Code Examples
- Basic usage:
import React, { useState } from "react";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
function Example() {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
/>
);
}
- Date range selection:
import React, { useState } from "react";
import DatePicker from "react-datepicker";
function DateRangeExample() {
const [dateRange, setDateRange] = useState([null, null]);
const [startDate, endDate] = dateRange;
return (
<DatePicker
selectsRange={true}
startDate={startDate}
endDate={endDate}
onChange={(update) => {
setDateRange(update);
}}
isClearable={true}
/>
);
}
- Custom input and styling:
import React, { useState } from "react";
import DatePicker from "react-datepicker";
function CustomInputExample() {
const [date, setDate] = useState(new Date());
return (
<DatePicker
selected={date}
onChange={(date) => setDate(date)}
customInput={<button className="custom-input">Select Date</button>}
wrapperClassName="custom-wrapper"
dayClassName={(date) =>
date.getDay() === 0 || date.getDay() === 6 ? "weekend" : undefined
}
/>
);
}
Getting Started
- Install the package:
npm install react-datepicker
- Import the component and styles:
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
- Use the component in your React application:
function MyComponent() {
const [selectedDate, setSelectedDate] = useState(null);
return (
<DatePicker
selected={selectedDate}
onChange={(date) => setSelectedDate(date)}
/>
);
}
Competitor Comparisons
An easily internationalizable, mobile-friendly datepicker library for the web
Pros of react-dates
- More comprehensive date range selection functionality
- Better support for internationalization and localization
- Extensive customization options for appearance and behavior
Cons of react-dates
- Larger bundle size due to additional features
- Steeper learning curve for implementation
- Less frequent updates and maintenance
Code Comparison
react-dates:
import { DateRangePicker } from 'react-dates';
<DateRangePicker
startDate={this.state.startDate}
endDate={this.state.endDate}
onDatesChange={({ startDate, endDate }) => this.setState({ startDate, endDate })}
focusedInput={this.state.focusedInput}
onFocusChange={focusedInput => this.setState({ focusedInput })}
/>
react-datepicker:
import DatePicker from "react-datepicker";
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
selectsStart
startDate={startDate}
endDate={endDate}
/>
The code comparison shows that react-dates offers a more comprehensive DateRangePicker component out of the box, while react-datepicker requires additional configuration for date range selection. react-dates also handles focus state management internally, which can simplify implementation in some cases.
Date & Time pickers for Material UI (support from v1 to v4)
Pros of material-ui-pickers
- Seamless integration with Material-UI components and theming
- Offers a wider range of date/time picker variants (e.g., mobile, desktop, static)
- Built-in localization support for multiple languages
Cons of material-ui-pickers
- Larger bundle size due to Material-UI dependencies
- Steeper learning curve for developers not familiar with Material-UI
- Less customizable compared to react-datepicker's flexible styling options
Code Comparison
material-ui-pickers:
import { DatePicker } from '@material-ui/pickers';
<DatePicker
label="Basic example"
value={selectedDate}
onChange={handleDateChange}
/>
react-datepicker:
import DatePicker from "react-datepicker";
<DatePicker
selected={startDate}
onChange={date => setStartDate(date)}
/>
Both libraries offer similar basic functionality, but material-ui-pickers follows Material Design principles and integrates more closely with the Material-UI ecosystem. react-datepicker provides a more lightweight and flexible solution that can be easily customized to fit various design requirements.
React Calendar
Pros of calendar
- More customizable and flexible, allowing for advanced use cases
- Supports multiple calendar views (month, year, decade)
- Lightweight and focused on core calendar functionality
Cons of calendar
- Less out-of-the-box styling and theming options
- Steeper learning curve for basic implementation
- Fewer built-in accessibility features
Code Comparison
react-datepicker:
import DatePicker from "react-datepicker";
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
dateFormat="MMMM d, yyyy"
/>
calendar:
import Calendar from 'rc-calendar';
<Calendar
onSelect={onSelect}
defaultValue={now}
showDateInput={false}
/>
react-datepicker provides a more straightforward implementation for basic date picking, while calendar offers more flexibility but requires additional configuration for similar functionality.
react-datepicker includes built-in date formatting and state management, whereas calendar leaves these aspects to the developer, allowing for more customization but requiring more setup.
Both libraries offer powerful date selection capabilities, but react-datepicker is generally easier to use for simple cases, while calendar shines in more complex scenarios requiring fine-grained control over the calendar display and behavior.
DayPicker is a customizable date picker component for React. Add date pickers, calendars, and date inputs to your web applications.
Pros of react-day-picker
- More customizable and flexible, allowing for advanced use cases
- Smaller bundle size, which can lead to better performance
- Provides a more accessible interface out of the box
Cons of react-day-picker
- Less opinionated styling, requiring more work to achieve a polished look
- Fewer built-in features compared to react-datepicker
- Steeper learning curve for beginners due to its flexibility
Code Comparison
react-day-picker:
import { DayPicker } from 'react-day-picker';
function MyDatePicker() {
return <DayPicker mode="single" />;
}
react-datepicker:
import DatePicker from "react-datepicker";
function MyDatePicker() {
return <DatePicker selected={new Date()} onChange={(date) => setDate(date)} />;
}
Both libraries offer simple implementations for basic date picking functionality. react-day-picker uses a more declarative approach with the mode
prop, while react-datepicker requires handling the selected date and onChange event explicitly. react-day-picker's API focuses on composability, allowing for more complex scenarios, whereas react-datepicker provides a more straightforward API for common use cases.
The Select Component for React.js
Pros of react-select
- More versatile, supporting various types of select inputs (single, multi, creatable)
- Extensive customization options for styling and behavior
- Better performance with large datasets due to virtualization
Cons of react-select
- Steeper learning curve due to more complex API
- Larger bundle size, which may impact load times for smaller projects
- Some users report issues with mobile device compatibility
Code Comparison
react-select:
import Select from 'react-select';
const options = [
{ value: 'chocolate', label: 'Chocolate' },
{ value: 'strawberry', label: 'Strawberry' },
{ value: 'vanilla', label: 'Vanilla' }
];
<Select options={options} />
react-datepicker:
import DatePicker from "react-datepicker";
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
/>
While both libraries offer React components for user input, react-select focuses on flexible select inputs, whereas react-datepicker specializes in date and time selection. react-select provides more customization options but may require more setup, while react-datepicker offers a simpler API for its specific use case.
lightweight, powerful javascript datetimepicker with no dependencies
Pros of flatpickr
- Lightweight and dependency-free, resulting in smaller bundle size
- Supports a wide range of date and time formats out of the box
- Easy to customize and extend with plugins
Cons of flatpickr
- Not specifically designed for React, may require additional setup
- Less integrated with React ecosystem and patterns
- May have fewer React-specific features and optimizations
Code Comparison
flatpickr:
flatpickr("#myDatePicker", {
enableTime: true,
dateFormat: "Y-m-d H:i",
onChange: function(selectedDates, dateStr, instance) {
console.log(dateStr);
}
});
react-datepicker:
import DatePicker from "react-datepicker";
<DatePicker
selected={startDate}
onChange={(date) => setStartDate(date)}
showTimeSelect
dateFormat="yyyy-MM-dd HH:mm"
/>
Both libraries offer similar functionality, but react-datepicker provides a more React-centric approach with props and state management. flatpickr, being more general-purpose, requires manual DOM selection and configuration. The react-datepicker example demonstrates better integration with React's component model and state handling.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
React Date Picker
A simple and reusable Datepicker component for React (Demo)
Installation
The package can be installed via npm:
npm install react-datepicker --save
Or via yarn:
yarn add react-datepicker
Youâll need to install React and PropTypes separately since those dependencies arenât included in the package. If you need to use a locale other than the default en-US, you'll also need to import that into your project from date-fns (see Localization section below). Below is a simple example of how to use the Datepicker in a React view. You will also need to require the CSS file from this package (or provide your own). The example below shows how to include the CSS from this package if your build system supports requiring CSS files (Webpack is one that does).
import React, { useState } from "react";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
// CSS Modules, react-datepicker-cssmodules.css
// import 'react-datepicker/dist/react-datepicker-cssmodules.css';
const Example = () => {
const [startDate, setStartDate] = useState(new Date());
return (
<DatePicker selected={startDate} onChange={(date) => setStartDate(date)} />
);
};
Configuration
The most basic use of the DatePicker can be described with:
<DatePicker selected={startdate} onChange={(date) => setStartDate(date)} />
You can use onSelect
event handler which fires each time some calendar date has been selected
<DatePicker
selected={date}
onSelect={handleDateSelect} //when day is clicked
onChange={handleDateChange} //only when value has changed
/>
onClickOutside
handler may be useful to close datepicker in inline
mode
See here for a full list of props that may be passed to the component. Examples are given on the main website.
Time picker
You can also include a time picker by adding the showTimeSelect prop
<DatePicker
selected={date}
onChange={handleDateChange}
showTimeSelect
dateFormat="Pp"
/>
Times will be displayed at 30-minute intervals by default (default configurable via timeIntervals prop)
More examples of how to use the time picker are given on the main website
Localization
The date picker relies on date-fns internationalization to localize its display components. By default, the date picker will use the locale globally set, which is English. Provided are 3 helper methods to set the locale:
- registerLocale (string, object): loads an imported locale object from date-fns
- setDefaultLocale (string): sets a registered locale as the default for all datepicker instances
- getDefaultLocale: returns a string showing the currently set default locale
import { registerLocale, setDefaultLocale } from "react-datepicker";
import { es } from 'date-fns/locale/es';
registerLocale('es', es)
<DatePicker
locale="es"
/>
Locales can be changed in the following way:
- Globally -
setDefaultLocale('es');
Compatibility
React
We're always trying to stay compatible with the latest version of React. We can't support all older versions of React.
Latest compatible versions:
- React 16 or newer: React-datepicker v2.9.4 and newer
- React 15.5: React-datepicker v2.9.3
- React 15.4.1: needs React-datepicker v0.40.0, newer won't work (due to react-onclickoutside dependencies)
- React 0.14 or newer: All above React-datepicker v0.13.0
- React 0.13: React-datepicker v0.13.0
- pre React 0.13: React-datepicker v0.6.2
Moment.js
Up until version 1.8.0, this package was using Moment.js. Starting v2.0.0, we switched to using date-fns
, which uses native Date objects, to reduce the size of the package. If you're switching from 1.8.0 to 2.0.0 or higher, please see the updated example above of check out the examples site for up to date examples.
Browser Support
The date picker is compatible with the latest versions of Chrome, Firefox, and IE10+.
Unfortunately, it is difficult to support legacy browsers while maintaining our ability to develop new features in the future. For IE9 support, it is known that the classlist polyfill is needed, but this may change or break at any point in the future.
Local Development
The main
branch contains the latest version of the Datepicker component.
To begin local development:
- Run
yarn link
from project root - Run
cd docs-site && yarn link react-datepicker
- Run
yarn install
from project root - Run
yarn build
from project root - Run
yarn start
from project root
The last step starts documentation app as a simple webserver on http://localhost:3000.
You can run yarn test
to execute the test suite and linters. To help you develop the component weâve set up some tests that cover the basic functionality (can be found in /tests
). Even though weâre big fans of testing, this only covers a small piece of the component. We highly recommend you add tests when youâre adding new functionality.
Please refer to CONTRIBUTING.md
file for more details about getting set up.
The examples
The examples are hosted within the docs folder and are ran in the simple app that loads the Datepicker. To extend the examples with a new example, you can simply duplicate one of the existing examples and change the unique properties of your example.
Accessibility
Keyboard support
- Left: Move to the previous day.
- Right: Move to the next day.
- Up: Move to the previous week.
- Down: Move to the next week.
- PgUp: Move to the previous month.
- Shift+PgUp: Move to the same day and month of the previous year. If that day does not exist, moves focus to the last day of the month.
- PgDn: Move to the next month.
- Shift+PgDn: Move to the same day and month of the next year. If that day does not exist, moves focus to the last day of the month.
- Home: Move to the first day (e.g Sunday) of the current week.
- End: Move to the last day (e.g. Saturday) of the current week.
- Enter/Esc/Tab: close the calendar. (Enter & Esc calls preventDefault)
For month picker
- Left: Move to the previous month.
- Right: Move to the next month.
- Enter: Select date and close the calendar
License
Copyright (c) 2014-2024 HackerOne Inc. and individual contributors. Licensed under MIT license, see LICENSE for the full license.
Top Related Projects
An easily internationalizable, mobile-friendly datepicker library for the web
Date & Time pickers for Material UI (support from v1 to v4)
React Calendar
DayPicker is a customizable date picker component for React. Add date pickers, calendars, and date inputs to your web applications.
The Select Component for React.js
lightweight, powerful javascript datetimepicker with no dependencies
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot