What is react-datetime?
The react-datetime package is a versatile and flexible date and time picker component for React applications. It allows users to select dates, times, or both, and provides various customization options to fit different use cases.
What are react-datetime's main functionalities?
Basic Date Picker
This feature allows users to select a date from a calendar view. The basic date picker is easy to implement and provides a straightforward way to choose dates.
import React from 'react';
import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
const BasicDatePicker = () => (
<Datetime />
);
export default BasicDatePicker;
Time Picker
This feature allows users to select a time without selecting a date. By setting the dateFormat prop to false, the component will only display the time picker.
import React from 'react';
import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
const TimePicker = () => (
<Datetime dateFormat={false} />
);
export default TimePicker;
Date and Time Picker
This feature combines both date and time selection in a single component. Users can pick both a date and a time, making it useful for scheduling events.
import React from 'react';
import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
const DateTimePicker = () => (
<Datetime />
);
export default DateTimePicker;
Custom Date Format
This feature allows customization of the date format. By setting the dateFormat prop, users can specify how the date should be displayed.
import React from 'react';
import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
const CustomDateFormat = () => (
<Datetime dateFormat="DD-MM-YYYY" />
);
export default CustomDateFormat;
Disable Past Dates
This feature disables the selection of past dates. By using the isValidDate prop, users can define a function to restrict date selection.
import React from 'react';
import Datetime from 'react-datetime';
import 'react-datetime/css/react-datetime.css';
const DisablePastDates = () => (
<Datetime isValidDate={(current) => current.isAfter(Datetime.moment().subtract(1, 'day'))} />
);
export default DisablePastDates;
Other packages similar to react-datetime
react-datepicker
react-datepicker is a popular date picker component for React. It offers a wide range of customization options and supports both date and time selection. Compared to react-datetime, react-datepicker has a more modern design and additional features like range selection and inline display.
react-day-picker
react-day-picker is a flexible date picker component for React. It provides extensive customization options and supports features like range selection, multiple date selection, and custom modifiers. Unlike react-datetime, react-day-picker focuses solely on date selection and does not include time picking functionality.
react-calendar
react-calendar is a simple and lightweight calendar component for React. It allows users to select dates and provides basic customization options. Compared to react-datetime, react-calendar is more lightweight and easier to use but lacks built-in time selection capabilities.
react-datetime
This project is a port of https://github.com/Eonasdan/bootstrap-datetimepicker for React.js
Usage
Installation :
npm install react-datetime
Then
require('react-datetime');
...
render: function() {
return <Datetime />;
}
See Examples for more details.
API
Name | Type | Default | Description |
---|
date | Date | new Date() | Represents the inital dateTime, this string is then parsed by moment.js |
dateFormat | string | "MM/DD/YY" | Defines the format moment.js should use to parse and output the date. The default is only set if there is not timeFormat defined. |
timeFormat | string | "MM/DD/YY" | Defines the format moment.js should use to parse and output the time. The default is only set if there is not dateFormat defined. |
onChange | function | x => console.log(x) | Callback trigger when the date changes |
viewMode | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
inputProps | object | undefined | Defines additional attributes for the input element of the component. |
minDate | moment | undefined | The earliest date allowed for entry in the calendar view. |
maxDate | moment | undefined | The latest date allowed for entry in the calendar view. |
Contributions
Any help is always welcome :)