React DateTime Picker
react-datetime-picker is a React component that provides a flexible and comprehensive solution for picking both dates and times. It is currently at stable version 7.0.2 and actively maintained, with frequent minor and patch releases, and major versions introducing modern practices like ESM-only builds. Key differentiators include its focus on integrating seamlessly into React applications, shipping with TypeScript types for improved developer experience, and embracing modern web standards and security features such as OIDC trusted publishing and npm provenance. The component supports a wide range of React versions, from 16.8.0 up to 19.0.0, and simplifies date and time input within forms.
Common errors
-
TypeError: get format called on incompatible undefined
cause This error was observed in some browsers, often related to how date formatting or locale utilities were accessed internally with potentially undefined values.fixUpgrade to react-datetime-picker v7.0.2 or newer, as this specific bug was fixed in that release. -
Failed prop type: DateTimePicker: prop type portalContainer is invalid
cause An issue with prop type validation for `portalContainer` when using server-side rendering (SSR), leading to validation errors during SSR.fixUpgrade to react-datetime-picker v5.5.2 or newer, where this server-side rendering specific prop type validation error was addressed. -
ReferenceError: HTMLElement is not defined
cause This error occurs during server-side rendering (SSR) when client-side DOM APIs like `HTMLElement` are accessed in a Node.js environment where they do not exist.fixUpgrade to react-datetime-picker v5.5.0 or newer. This version specifically addressed `HTMLElement is not defined` errors during SSR by ensuring DOM-specific code is not executed on the server. -
Jest Encountered an unexpected token 'export ... from ...'
cause After upgrading to v7.0.0, Jest-based test setups may fail when trying to import `react-datetime-picker` because the library is now ESM-only, and Jest's default configuration struggles with ESM.fixConfigure Jest to transpile ESM modules or consider migrating to a test runner like Vitest that natively supports ESM. This might involve using `jest-esm-transformer` or updating your `package.json` to include `"type": "module"` and configuring Jest accordingly.
Warnings
- breaking As of v7.0.0, react-datetime-picker has dropped CommonJS build support and is now ESM-only. Applications using CommonJS (e.g., older Node.js environments or certain test runners like Jest) will need to adapt.
- breaking Version 6.0.0 introduced a requirement for the New JSX Transform in React. If your project is not configured for it, components might fail to render or build.
- breaking The `propTypes` mechanism was entirely removed in v6.0.0. Projects relying on `propTypes` for validation will lose this functionality.
- gotcha After upgrading to v7.0.0, users running unit tests with Jest might encounter issues due to the package becoming ESM-only, as Jest's ESM support can be challenging.
- gotcha The package adopted OIDC trusted publishing (v7.0.2) and npm provenance statements (v5.5.3). While this enhances supply chain security, it's a notable change in publishing metadata that tools relying on specific package integrity checks might need to be aware of.
Install
-
npm install react-datetime-picker -
yarn add react-datetime-picker -
pnpm add react-datetime-picker
Imports
- DateTimePicker
const DateTimePicker = require('react-datetime-picker');import DateTimePicker from 'react-datetime-picker';
- DateTimePickerProps
import type { DateTimePickerProps } from 'react-datetime-picker'; - value
const [value, setValue] = useState(new Date()); <DateTimePicker onChange={setValue} value={value} />const [value, onChange] = useState(new Date()); <DateTimePicker onChange={onChange} value={value} />
Quickstart
import React, { useState } from 'react';
import DateTimePicker from 'react-datetime-picker';
import 'react-datetime-picker/dist/DateTimePicker.css';
import 'react-calendar/dist/Calendar.css';
import 'react-clock/dist/Clock.css';
interface MyDateTimePickerProps {}
const MyDateTimePicker: React.FC<MyDateTimePickerProps> = () => {
const [value, onChange] = useState<Date | null>(new Date());
return (
<div>
<h1>Select Date and Time</h1>
<DateTimePicker
onChange={onChange}
value={value}
clearIcon={null}
calendarIcon={null}
format="y-MM-dd HH:mm:ss"
disableClock={false}
locale="en-US"
/>
{
value && (
<p>Selected: {value.toLocaleString()}</p>
)
}
</div>
);
};
export default MyDateTimePicker;