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 is highly customizable and it even allows to edit 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.
Install using npm:
npm install --save react-datetime
React.js and Moment.js are peer dependencies for react-datetime. These dependencies are not installed along with react-datetime automatically, but your project needs to have them installed in order to make the datepicker work. You can then use the datepicker like in the example below.
require('react-datetime');
...
render: function() {
  return <Datetime />;
}Don't forget to add the CSS stylesheet to make it work out of the box.
| Name | Type | Default | Description | 
|---|---|---|---|
| value | Date | new Date() | Represents the selected date by the component, in order to use it as a controlled component. This prop is parsed by Moment.js, so it is possible to use a date stringor amomentobject. | 
| defaultValue | Date | new Date() | Represents the selected date 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 stringor amomentobject. | 
| dateFormat | booleanorstring | true | Defines the format for the date. It accepts any Moment.js date format (not in localized format). If truethe date will be displayed using the defaults for the current locale. Iffalsethe datepicker is disabled and the component can be used as timepicker. | 
| timeFormat | booleanorstring | true | Defines the format for the time. It accepts any Moment.js time format (not in localized format). If truethe time will be displayed using the defaults for the current locale. Iffalsethe timepicker is disabled and the component can be used as datepicker. | 
| input | boolean | true | Whether to show an input field to edit the date manually. | 
| open | boolean | null | Whether to open or close the picker. If not set react-datetime will open the datepicker on input focus and close it on click outside. | 
| 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. | 
| utc | boolean | false | When true, input time values will be interpreted as UTC (Zulu time) by Moment.js. Otherwise they will default to the user's local timezone. | 
| onChange | function | empty function | Callback trigger when the date changes. The callback receives the selected momentobject as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback receives the value of the input (a string). | 
| onFocus | function | empty function | Callback trigger for when the user opens the datepicker. | 
| onBlur | function | empty function | Callback trigger for when the user clicks outside of the input, simulating a regular onBlur. The callback receives the selected momentobject as only parameter, if the date in the input is valid. If the date in the input is not valid, the callback returned. | 
| viewMode | stringornumber | 'days' | The default view to display when the picker is shown ( 'years','months','days','time'). | 
| className | stringorstring array | '' | Extra class name for the outermost markup element. | 
| inputProps | object | undefined | Defines additional attributes for the input element of the component. For example: placeholder,disabled,requiredandname. | 
| isValidDate | function | () => true | Define the dates that can be selected. The function receives (currentDate, selectedDate)and should return atrueorfalsewhether thecurrentDateis valid or not. See selectable dates. | 
| renderDay | function | DOM.td(day) | Customize the way that the days are shown in the daypicker. The accepted function has the selectedDate, the current date and the default calculatedpropsfor 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 monthpicker. The accepted function has the selectedDate, the current date and the default calculatedpropsfor the cell, themonthand theyearto be shown, 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 calculatedpropsfor the cell, theyearto be shown, and must return a React component. See appearance customization. | 
| strictParsing | boolean | false | Whether to use Moment.js's strict parsing when parsing input. | 
| closeOnSelect | boolean | false | When true, once the day has been selected, the datepicker will be automatically closed. | 
| closeOnTab | boolean | true | When trueand the input is focused, pressing thetabkey will close the datepicker. | 
| timeConstraints | object | null | Add some constraints to the timepicker. It accepts an objectwith the format{ hours: { min: 9, max: 15, step: 2 }}, this example means the hours can't be lower than9and higher than15, and it will change adding or subtracting2hours everytime the buttons are clicked. The constraints can be added to thehours,minutes,secondsandmilliseconds. | 
| disableOnClickOutside | boolean | false | When true, keep the datepicker open when click event is triggered outside of component. Whenfalse, close it. | 
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 frenchIf 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 datepicker display the days, months and years in the calendar. To adapt the calendar for every need it is possible to use the props renderDay(props, currentDate, selectedDate), renderMonth(props, month, year, selectedDate) and renderYear(props, year, selectedDate) to customize the output of each rendering method.
var MyDTPicker = React.createClass({
    render: function(){
        return <Datetime
            renderDay={ this.renderDay }
            renderMonth={ this.renderMonth }
            renderYear={ this.renderYear }
        />;
    },
    renderDay: function( props, currentDate, selectedDate ){
        return <td {...props}>{ '0' + currentDate.date() }</td>;
    },
    renderMonth: function( props, month, year, selectedDate){
        return <td {...props}>{ month }</td>;
    },
    renderYear: function( props, year, selectedDate ){
        return <td {...props}>{ year % 100 }</td>;
    }
});You can see a customized calendar here.
- propsis the object that the datepicker has calculated for this object. It is convenient to use this object as the- propsfor your custom component, since it knows how to handle the click event and its- classNameattribute is used by the default styles.
- selectedDateand- currentDateare moment objects and can be used to change the output depending on the selected date, or the date for the current day.
- monthand- yearare the numeric representation of the current month and year to be displayed. Notice that the possible- monthvalues range from- 0to- 11.
It is possible to disable dates in the calendar if the user are not allowed to select them. It is done using the prop isValidDate, which admits a function in the form function(currentDate, selectedDate) where both arguments are moment objects. The function should return true for selectable dates, and false for disabled ones.
In the example below are all dates before today disabled.
// Let's use the static moment reference in the Datetime component
var yesterday = Datetime.moment().subtract(1, 'day');
var valid = function( current ){
    return current.isAfter( yesterday );
};
<Datetime isValidDate={ valid } />Working example of disabled days here.
It's also possible to disable the weekends, as shown in the example below.
var valid = function( current ){
    return current.day() !== 0 && current.day() !== 6;
};
<Datetime isValidDate={ valid } />Working example of disabled weekends here.
This project includes typings for TypeScript versions 1.8 and 2.0. Additional typings are not required.
Typings for 1.8 are found in react-datetime.d.ts and typings for 2.0 are found in typings/index.d.ts.
import * as Datetime  from 'react-datetime';
class MyDTPicker extends React.Component<MyDTPickerProps, MyDTPickerState> {
    render() JSX.Element {
        return <Datetime />;
    }
}- For information about how to contribute, see the CONTRIBUTING file.
- For development we recommend that you use react-datetime-playground.