Skip to content

Commit cbe6442

Browse files
committed
Updated docs for date&time formats
2 parents baa9f18 + 839cd84 commit cbe6442

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

DateTime.js

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ var Datetime = React.createClass({
2828
onChange: TYPES.func,
2929
locale: TYPES.string,
3030
input: TYPES.bool,
31-
dateFormat: TYPES.string,
32-
timeFormat: TYPES.string,
31+
// dateFormat: TYPES.string | TYPES.bool,
32+
// timeFormat: TYPES.string | TYPES.bool,
3333
inputProps: TYPES.object,
3434
viewMode: TYPES.oneOf(['years', 'months', 'days', 'time']),
3535
isValidDate: TYPES.func,
@@ -45,15 +45,22 @@ var Datetime = React.createClass({
4545
inputProps: {},
4646
input: true,
4747
onBlur: nof,
48-
onChange: nof
48+
onChange: nof,
49+
timeFormat: true,
50+
dateFormat: true
4951
};
5052
},
5153
getInitialState: function() {
5254
var formats = this.getFormats( this.props ),
53-
date = this.props.date
55+
date = this.props.date,
56+
currentView = this.props.viewMode
5457
;
58+
59+
if( !formats.date )
60+
currentView = 'time';
61+
5562
return {
56-
currentView: this.props.viewMode,
63+
currentView: currentView,
5764
open: !this.props.input,
5865
inputFormat: formats.datetime,
5966
viewDate: this.localMoment(date).startOf("month"),
@@ -64,38 +71,21 @@ var Datetime = React.createClass({
6471

6572
getFormats: function( props ){
6673
var formats = {
67-
date: '',
68-
time: '',
69-
datetime: ''
74+
date: props.dateFormat || '',
75+
time: props.timeFormat || ''
7076
},
7177
locale = this.localMoment( props.date ).localeData()
7278
;
7379

74-
if( props.dateFormat ){
75-
formats.date = props.dateFormat;
76-
}
77-
if( props.timeFormat ){
78-
formats.time = props.timeFormat;
79-
}
80-
81-
if( !formats.date && !formats.time ){
80+
if( formats.date === true ){
8281
formats.date = locale.longDateFormat('L');
83-
formats.time = locale.longDateFormat('LT');
84-
formats.datetime = formats.date + ' ' + formats.time;
8582
}
86-
else {
87-
if( props.dateFormat ){
88-
formats.date = props.dateFormat;
89-
formats.datetime = formats.date;
90-
}
91-
if( props.timeFormat ){
92-
if( formats.date )
93-
formats.datetime += ' ';
94-
formats.time = props.timeFormat;
95-
formats.datetime += formats.time;
96-
}
83+
if( formats.time === true ){
84+
formats.time = locale.longDateFormat('LT');
9785
}
9886

87+
formats.datetime = formats.date + ' ' + formats.time;
88+
9989
return formats;
10090
},
10191

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ API
3232
| Name | Type | Default | Description |
3333
| ------------ | ------- | ------- | ----------- |
3434
| **date** | Date | new Date() | Represents the inital dateTime, this prop is parsed by moment.js, so it is possible to use a date string. |
35-
| **dateFormat** | string | Locale default | Defines the format moment.js should use to parse and output the date. The default is only set if there is not `timeFormat` defined. |
36-
| **timeFormat** | string | Locale default | Defines the format moment.js should use to parse and output the time. The default is only set if there is not `dateFormat` defined. |
35+
| **dateFormat** | `bool` or `string` | `true` | Defines the format for the date. It accepts any [moment.js date format](http://momentjs.com/docs/#/displaying/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. |
36+
| **timeFormat** | `bool` or `string` | `true` | Defines the format for the time. It accepts any [moment.js time format](http://momentjs.com/docs/#/displaying/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. |
3737
| **input** | boolean | true | Wether to show an input field to edit the date manually. |
3838
| **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](#i18n).
3939
| **onChange** | function | empty function | Callback trigger when the date changes. The callback receives the selected `moment` object as only parameter. |

0 commit comments

Comments
 (0)