Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
react-datetime
Advanced tools
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.
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;
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 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 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.
A date and time picker in the same React.js component. It can be used as a datepicker, timepicker or both at the same time.
It allows to edit even date's milliseconds.
This project started as a fork of https://github.com/quri/react-bootstrap-datetimepicker but the code and the API has changed a lot.
Installation :
npm install react-datetime
Then
require('react-datetime');
...
render: function() {
return <Datetime />;
}
Name | Type | Default | Description |
---|---|---|---|
value | Date | new Date() | Represents the value for the compones, in order to use it as a controlled component. This prop is parsed by moment.js, so it is possible to use a date string or a moment.js date. |
defaultValue | Date | new Date() | Represents the inital value for the component to use it as a uncontrolled component. This prop is parsed by moment.js, so it is possible to use a date string or a moment.js date. |
dateFormat | bool or string | true | Defines the format for the date. It accepts any moment.js date format. If true the date will be displayed using the defaults for the current locale. If false the datepicker is disabled and the component can be used as timepicker. |
timeFormat | bool or string | true | Defines the format for the time. It accepts any moment.js time format. If true the time will be displayed using the defaults for the current locale. If false the timepicker is disabled and the component can be used as datepicker. |
input | boolean | true | Wether to show an input field to edit the date manually. |
locale | string | null | Manually set the locale for the react-datetime instance. Moment.js locale needs to be loaded to be used, see i18n docs. |
onChange | function | empty function | Callback trigger when the date changes. The callback receives the selected moment object as only parameter. |
onBlur | function | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected moment object as only parameter. |
viewMode | string or number | 'days' | The default view to display when the picker is shown. ('years', 'months', 'days', 'time') |
className | string | "" | Extra class names for the component markup. |
inputProps | object | undefined | Defines additional attributes for the input element of the component. |
isValidDate | function | () => true | Define the dates that can be selected. The function receives (currentDate, selectedDate) and should return a true or false whether the currentDate is valid or not. See selectable dates. |
renderDay | function | DOM.td( day ) | Customize the way that the days are shown in the day picker. The accepted function has the selectedDate , the current date and the default calculated props for the cell, and must return a React component. See appearance customization |
renderMonth | function | DOM.td( month ) | Customize the way that the months are shown in the month picker. The accepted function has the selectedDate , the current date and the default calculated props for the cell, and must return a React component. See appearance customization |
renderYear | function | DOM.td( year ) | Customize the way that the years are shown in the year picker. The accepted function has the selectedDate , the current date and the default calculated props for the cell, and must return a React component. See appearance customization |
Different language and date formats are supported by react-datetime. React uses moment.js to format the dates, and the easiest way of changing the language of the calendar is changing the moment.js locale.
var moment = require('moment');
require('moment/locale/fr');
// Now react-datetime will be in french
If there are multiple locales loaded, you can use the prop locale
to define what language should be used by the instance:
<Datetime locale="fr-ca" />
<Datetime locale="de" />
Here you can see the i18n example working.
It is possible to customize the way that the datetime picker display the days, months and years in the calendar. To adapt the calendar to every need it is possible to use the props renderDay( props, currentDate, selectedDate )
, renderMonth( props, month, year, selectedDate )
and renderYear( props, year, selectedDate )
of react-datetime.
var MyDTPicker = React.createClass({
render: function(){
return <Datetime
renderDay={ this.renderDay }
renderMonth={ this.renderMonth }
renderYear={ this.renderYear }
/>;
},
renderDay: function( selectedDate, currentDate, props ){
return <td {...props}>{ currentDate.date() }</td>;
},
renderMonth: function( selectedDate, currentMonthDate, props ){
return <td {...props}>{ month }</td>;
},
renderDay: function( selectedDate, year, props ){
return <td {...props}>{ currentDate.date() }</td>;
}
});
props
is the object that react-date picker has calculated for this object. It is convenient to use this object as the props for your custom component, since it knows how to handle the click event and its className
attribute is used by the default styles.selectedDate
and currentDate
are Moment.js objects and can be used to change the output depending on the selected date, or the date for the current day.month
and year
are the numeric representation of the current month and year to be displayed. Notice that the possible month
values go from 0
to 11
.It is possible to disable dates in the calendar if we don't want the user to select them. It is possible thanks to the prop isValidDate
, which admits a function in the form function( currentDate, selectedDate )
where both arguments are moment.js objects. The function should return true
for selectable dates, and false
for disabled ones.
If we want to disable all the dates before today we can do like
var valid = function( current ){
var limit = new Date(); // Today
// Yesterday
limit.setDate(limit.getDate() - 1);
// Only dates after yesterday are ok
return current > limit;
};
<Datetime isValidDate={ valid } />
If we want only odd dates to be valid we could do like
var valid = function( current ){
return current.date() % 2;
};
<Datetime isValidDate={ valid } />
Any help is always welcome :)
1.0.0-rc.1
This is the release candidate for this project. Now it is pretty usable and API won't change drastically in a while. If you were using the alpha versions (v0.x) there is a bunch of breaking changes:
date
prop is now called defaultValue
and it is the initial value to use the component uncontrolled.value
prop has been added to use it as a controlled component.minDate
and maxDate
props. Now to define what dates are valid it is possible to use the new isValidDate
prop.dateFormat
and timeFormat
default value is always the locale default format. In case that you don't want the component to show the date/time picker you should set dateFormat
/timeFormat
to false
.Moreover:
className
prop has been added to customize component class.FAQs
A lightweight but complete datetime picker React.js component
The npm package react-datetime receives a total of 199,210 weekly downloads. As such, react-datetime popularity was classified as popular.
We found that react-datetime demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.