Skip to content

Commit 34ddb06

Browse files
Inbal-Tishethanshar
authored andcommitted
DateTimePicker - Adding props for date and time formats (#620)
* Adding date and time formats to control input text formats display * get props from themeProps
1 parent e00f03e commit 34ddb06

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

demo/src/screens/componentScreens/DateTimePickerScreen.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ export default class DateTimePickerScreen extends Component {
1212
<DateTimePicker
1313
title={'Date'}
1414
placeholder={'Select a date'}
15+
dateFormat={'MMM D, YYYY'}
1516
// value={new Date('October 13, 2014')}
1617
/>
1718
<DateTimePicker
1819
mode={'time'}
1920
title={'Time'}
2021
placeholder={'Select time'}
22+
timeFormat={'h:mm A'}
2123
// value={new Date('2015-03-25T12:00:00-06:30')}
2224
/>
2325
</View>

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"hoist-non-react-statics": "^3.0.0",
4646
"lodash": "^4.0.0",
4747
"memoize-one": "^5.0.5",
48+
"moment": "^2.24.0",
4849
"prop-types": "^15.5.10",
4950
"react-native-animatable": "^1.1.0",
5051
"react-native-color": "0.0.10",

src/components/dateTimePicker/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash';
22
import PropTypes from 'prop-types';
3+
import moment from 'moment';
34
import React from 'react';
45
import {StyleSheet} from 'react-native';
56
import RNDateTimePicker from '@react-native-community/datetimepicker';
@@ -52,7 +53,15 @@ class DateTimePicker extends BaseComponent {
5253
/**
5354
* The maximum date or time value to use
5455
*/
55-
maximumDate: PropTypes.instanceOf(Date)
56+
maximumDate: PropTypes.instanceOf(Date),
57+
/**
58+
* The date format for the text display
59+
*/
60+
dateFormat: PropTypes.string,
61+
/**
62+
* The time format for the text display
63+
*/
64+
timeFormat: PropTypes.string
5665
}
5766

5867
static defaultProps = {
@@ -158,7 +167,6 @@ class DateTimePicker extends BaseComponent {
158167
onChange={this.setDate}
159168
minimumDate={minimumDate}
160169
maximumDate={maximumDate}
161-
is24Hour // Android only
162170
/>
163171
);
164172
}
@@ -170,10 +178,12 @@ class DateTimePicker extends BaseComponent {
170178

171179
render() {
172180
const {chosenDate} = this.state;
173-
const {mode} = this.props;
181+
const {mode, dateFormat, timeFormat} = this.getThemeProps();
174182
const textInputProps = TextField.extractOwnProps(this.getThemeProps());
175-
const dateString = mode === MODES.DATE ? chosenDate.toLocaleDateString() : chosenDate.toLocaleTimeString();
176-
183+
const dateString = mode === MODES.DATE ?
184+
(dateFormat ? moment(chosenDate).format(dateFormat) : chosenDate.toLocaleDateString()) :
185+
(timeFormat ? moment(chosenDate).format(timeFormat) : chosenDate.toLocaleTimeString());
186+
177187
return (
178188
<TextField
179189
{...textInputProps}

0 commit comments

Comments
 (0)