Skip to content

Commit 905dfbd

Browse files
committed
feat: add getDateStr prop to customize display outside moment format
1 parent 58ed2b1 commit 905dfbd

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ You can check [index.js](https://github.com/xgfe/react-native-datepicker/blob/ma
9393
| onPressMask | - | `function` | This is called when clicking the ios modal mask |
9494
| modalOnResponderTerminationRequest | - | `function` | Set the callback for React Native's [Gesture Responder System](https://facebook.github.io/react-native/docs/gesture-responder-system.html#responder-lifecycle)'s call to `onResponderTerminationRequest`. By default this will reject a termination request, but can be overidden in case the View under the Modal is implementing custom gesture responders, and you wish for those to be overidden in certain cases. |
9595
| TouchableComponent | `TouchableHighlight` | `Component` | Replace the `TouchableHighlight` with a custom `Component`. For example : `TouchableOpacity` |
96+
| getDateStr | - | Function | A function to override how to format the date into a `String` for display, receives a `Date` instance
9697

9798
### Property `customStyles` available keys
9899

datepicker.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,15 @@ class DatePicker extends Component {
158158
getDateStr(date = this.props.date) {
159159
const {mode, format = FORMATS[mode]} = this.props;
160160

161-
if (date instanceof Date) {
162-
return Moment(date).format(format);
163-
} else {
164-
return Moment(this.getDate(date)).format(format);
161+
const dateInstance = date instanceof Date
162+
? date
163+
: this.getDate(date);
164+
165+
if (typeof this.props.getDateStr === 'function') {
166+
return this.props.getDateStr(dateInstance);
165167
}
168+
169+
return Moment(dateInstance).format(format);
166170
}
167171

168172
datePicked() {
@@ -471,7 +475,8 @@ DatePicker.propTypes = {
471475
onPressMask: PropTypes.func,
472476
placeholder: PropTypes.string,
473477
modalOnResponderTerminationRequest: PropTypes.func,
474-
is24Hour: PropTypes.bool
478+
is24Hour: PropTypes.bool,
479+
getDateStr: PropTypes.func
475480
};
476481

477482
export default DatePicker;

0 commit comments

Comments
 (0)