react-date-picker、react-datepicker 和 react-datetime 都是 React 生态中流行的日期选择组件,但它们在 API 设计、维护状态和功能侧重点上有所不同。react-datepicker 是目前社区采用率最高的标准方案,提供丰富的配置项和广泛的插件支持。react-date-picker(由 Wojtek Maj 维护)基于 react-calendar 构建,以简洁的输入框加日历图标样式著称,适合追求现代化 UI 的场景。react-datetime 早期非常流行,支持日期和时间混合选择,但近年来维护频率较低,可能面临兼容性风险。
在前端开发中,日期选择器是表单交互的核心组件之一。react-date-picker、react-datepicker 和 react-datetime 都试图解决相同的问题,但它们的实现思路和维护现状差异巨大。本文将从架构设计、API 用法、自定义能力及维护风险四个维度进行深度剖析。
这三个包都遵循 React 的受控组件模式,但在属性命名和数据结构上存在关键区别。
react-datepicker 使用 selected 和 onChange。
selected 接收一个 Date 对象。onChange 回调返回选中的 Date 对象。value 略有不同,需要适应。// react-datepicker
import DatePicker from 'react-datepicker';
function Example() {
const [date, setDate] = useState(new Date());
return (
<DatePicker
selected={date}
onChange={(date) => setDate(date)}
/>
);
}
react-date-picker 使用 value 和 onChange。
value 可以是 Date 对象或 [Date, Date] 数组(范围选择)。// react-date-picker
import DatePicker from 'react-date-picker';
function Example() {
const [value, setValue] = useState(new Date());
return (
<DatePicker
value={value}
onChange={setValue}
/>
);
}
react-datetime 使用 value 和 onChange。
Datetime 而非 DatePicker。// react-datetime
import Datetime from 'react-datetime';
function Example() {
const [value, setValue] = useState(new Date());
return (
<Datetime
value={value}
onChange={setValue}
/>
);
}
在实际工程中,默认样式往往无法满足设计稿需求,因此自定义能力至关重要。
react-datepicker 提供 customInput 属性。
// react-datepicker
<DatePicker
selected={date}
onChange={setDate}
customInput={<input className="custom-input" />}
renderCustomHeader={({ date, changeMonth, changeYear }) => (
<div>{date.getFullYear()}</div>
)}
/>
react-date-picker 通过 clearIcon、calendarIcon 等属性定制。
inputProps。// react-date-picker
<DatePicker
value={value}
onChange={setValue}
clearIcon={null}
calendarIcon={<span>📅</span>}
inputProps={{ className: 'my-input' }}
/>
react-datetime 支持 inputProps 和 renderInput。
// react-datetime
<Datetime
value={value}
onChange={setValue}
inputProps={{ className: 'my-input', placeholder: 'Select date' }}
/>
是否需要选择具体时间(小时、分钟)是选型的关键分水岭。
react-datepicker 需要显式开启时间选择。
showTimeSelect 属性启用时间。timeIntervals。// react-datepicker
<DatePicker
selected={date}
onChange={setDate}
showTimeSelect
timeIntervals={15}
dateFormat="yyyy-MM-dd HH:mm"
/>
react-date-picker 专注于日期。
react-datetime-picker 包。// react-date-picker (仅日期)
<DatePicker value={date} onChange={setDate} />
// 需引入兄弟包支持时间
// import DateTimePicker from 'react-datetime-picker';
react-datetime 默认包含时间。
dateFormat 和 timeFormat 控制显示。// react-datetime
<Datetime
value={date}
onChange={setDate}
dateFormat="YYYY-MM-DD"
timeFormat="HH:mm"
/>
对于长期项目,库的维护活跃度直接影响技术债务。
react-datepicker 维护活跃。
// react-datepicker
// 拥有完善的 TypeScript 类型定义
import DatePicker from 'react-datepicker';
// 类型支持良好,IDE 提示准确
react-date-picker 维护稳定。
react-calendar,生态独立且稳定。// react-date-picker
// 同样拥有良好的 TypeScript 支持
import DatePicker from 'react-date-picker';
// 结构清晰,依赖链简单
react-datetime 维护频率较低。
// react-datetime
// 需注意潜在的类型定义缺失或更新滞后
import Datetime from 'react-datetime';
// 建议在使用前检查最新 Issue 中的兼容性反馈
| 特性 | react-datepicker | react-date-picker | react-datetime |
|---|---|---|---|
| API 风格 | selected / onChange | value / onChange | value / onChange |
| 默认 UI | 输入框 + 弹出日历 | 输入框 + 图标 + 弹出日历 | 输入框 + 图标 + 弹出日历 |
| 时间支持 | 配置开启 (showTimeSelect) | 需兄弟包 (react-datetime-picker) | 默认支持 |
| 自定义输入 | 强 (customInput) | 中 (inputProps) | 中 (inputProps) |
| 维护状态 | 🟢 活跃 | 🟢 稳定 | 🟡 较低 |
| TypeScript | ✅ 完善 | ✅ 完善 | ⚠️ 一般 |
react-datepicker 是通用性最强的选择 🧰。如果你需要一个能应对各种复杂需求(范围选择、时间选择、自定义渲染)且社区资源丰富的库,它是首选。它的配置项虽然多,但文档齐全,适合大多数业务场景。
react-date-picker 是现代化 UI 的优选 🎨。如果你喜欢它默认的输入框加图标样式,且希望依赖一个结构清晰、基于 react-calendar 的稳定内核,它非常适合。注意若需时间选择,需引入额外包。
react-datetime 需谨慎评估 ⚠️。虽然它曾经很流行,且默认支持时间选择很方便,但考虑到维护频率和生态活跃度,在新项目中建议优先考察前两者。仅在维护旧系统或有特定兼容性需求时保留使用。
最终建议:对于新启动的 React 项目,优先在 react-datepicker 和 react-date-picker 之间根据 UI 风格偏好进行选择。
选择 react-datepicker 如果你需要最广泛的社区支持、丰富的自定义选项(如自定义输入框、头部渲染)以及良好的文档。它是大多数项目的安全默认选择,尤其适合需要复杂日期范围或特定交互逻辑的企业级应用。
选择 react-date-picker 如果你需要简洁现代的默认样式,且希望组件基于 react-calendar 内核以获得稳定的日历交互体验。它适合后台管理系统或需要清晰日期输入的场景,但注意它主要专注于日期选择,时间选择需使用其兄弟包。
选择 react-datetime 仅当你需要维护旧项目或依赖其特定的日期时间混合输入行为。对于新项目,建议谨慎评估其维护状态,因为相比其他两个包,其更新频率较低,可能存在与新版本 React 的兼容性隐患。
A simple and reusable Datepicker component for React (Demo)

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)} />;
};
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.
When using examples from the documentation site, note that they may reference utilities from external libraries. Common imports you might need:
Date manipulation (from date-fns):
import { getYear, getMonth, addDays, subDays, setHours, setMinutes } from "date-fns";
Utility functions:
range() function used in custom headers: import range from "lodash/range";const range = (start, end, step) => Array.from({ length: (end - start) / step }, (_, i) => start + i * step);TypeScript types:
import type { ReactDatePickerCustomHeaderProps } from "react-datepicker";
All examples on the documentation site include commented import statements at the top showing exactly what you need to import for your own project.
For a comprehensive guide on imports, see the Common Imports Guide.
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
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:
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:
setDefaultLocale('es');React-datepicker uses native JavaScript Date objects which are timezone-aware. By default, dates are displayed in the user's local timezone. The library does not include built-in timezone conversion utilities.
Common issue: "Date is one day off" (#1018) - If you're seeing dates shift by one day when converting to ISO strings or sending to a server, this is due to timezone conversion, not a bug. See the Timezone Handling Guide for solutions.
For detailed information about working with timezones, UTC dates, and common timezone-related scenarios, see the Timezone Handling Guide.
For applications requiring timezone conversion, we recommend using date-fns-tz alongside react-datepicker.
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:
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.
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.
The main branch contains the latest version of the Datepicker component.
To begin local development:
yarn install from project rootyarn build from project rootyarn start from project rootThe last step starts documentation app as a simple webserver on http://localhost:5173.
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 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.
Copyright (c) 2014-2025 HackerOne Inc. and individual contributors. Licensed under MIT license, see LICENSE for the full license.