Skip to content

DateTimePicker - Adding props for date and time formats #620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions demo/src/screens/componentScreens/DateTimePickerScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ export default class DateTimePickerScreen extends Component {
<DateTimePicker
title={'Date'}
placeholder={'Select a date'}
dateFormat={'MMM D, YYYY'}
// value={new Date('October 13, 2014')}
/>
<DateTimePicker
mode={'time'}
title={'Time'}
placeholder={'Select time'}
timeFormat={'h:mm A'}
// value={new Date('2015-03-25T12:00:00-06:30')}
/>
</View>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"hoist-non-react-statics": "^3.0.0",
"lodash": "^4.0.0",
"memoize-one": "^5.0.5",
"moment": "^2.24.0",
"prop-types": "^15.5.10",
"react-native-animatable": "^1.1.0",
"react-native-color": "0.0.10",
Expand Down
20 changes: 15 additions & 5 deletions src/components/dateTimePicker/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import PropTypes from 'prop-types';
import moment from 'moment';
import React from 'react';
import {StyleSheet} from 'react-native';
import RNDateTimePicker from '@react-native-community/datetimepicker';
Expand Down Expand Up @@ -52,7 +53,15 @@ class DateTimePicker extends BaseComponent {
/**
* The maximum date or time value to use
*/
maximumDate: PropTypes.instanceOf(Date)
maximumDate: PropTypes.instanceOf(Date),
/**
* The date format for the text display
*/
dateFormat: PropTypes.string,
/**
* The time format for the text display
*/
timeFormat: PropTypes.string
}

static defaultProps = {
Expand Down Expand Up @@ -158,7 +167,6 @@ class DateTimePicker extends BaseComponent {
onChange={this.setDate}
minimumDate={minimumDate}
maximumDate={maximumDate}
is24Hour // Android only
/>
);
}
Expand All @@ -170,10 +178,12 @@ class DateTimePicker extends BaseComponent {

render() {
const {chosenDate} = this.state;
const {mode} = this.props;
const {mode, dateFormat, timeFormat} = this.getThemeProps();
const textInputProps = TextField.extractOwnProps(this.getThemeProps());
const dateString = mode === MODES.DATE ? chosenDate.toLocaleDateString() : chosenDate.toLocaleTimeString();

const dateString = mode === MODES.DATE ?
(dateFormat ? moment(chosenDate).format(dateFormat) : chosenDate.toLocaleDateString()) :
(timeFormat ? moment(chosenDate).format(timeFormat) : chosenDate.toLocaleTimeString());

return (
<TextField
{...textInputProps}
Expand Down