xgfe / react-native-datepicker

react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS
MIT License
2.12k stars 725 forks source link

Deprecation Warning: not recognized RFC2822 or ISO format. #321

Open RBrNx opened 5 years ago

RBrNx commented 5 years ago

Issue

When using the DatePicker (even the example shown on the Github page) a warning is issue by Moment.js Deprecation warning: value provided is not in a recognized RFC2822 or ISO format.

screenshot_20190215-132904

The warning can be seen in the moment.js docs here: http://momentjs.com/guides/#/warnings/js-date/

Expected Behavior

No warnings should appear when using the component

Environment

  1. react-native -v: ^0.57.8
  2. node -v: 10.14.2
  3. npm -v: 6.4.1
  4. target platform: Android | iOS
  5. operating system: Windows
crice88 commented 5 years ago

I ran into this issue as well. I believe it has to do with the fact that the format prop of datepicker mutates the date. The onDateChange then receives a string date that is not parsable.

It would be nice if the format just displayed it in whatever format I wanted but didn't actually change the underlying date. That way when onDateChange is fired, the date is still in a consumable format.

zvs001 commented 5 years ago

I can suggest to use ISO format of result date. I fixed my issues this way:

const format = "hh:MMM, DD MMMM, hh:mma" // crazy format

class DefaultTimePicker extends React.PureComponent {

  render() {
    const { selectedValue } = this.props

    let value = null
    if(selectedValue) value = moment(selectedValue).format(format)

    return (
      <DatePicker
        date={value}
        format={format}
        onDateChange={this.handleChange}
      />
    )
  }

  handleChange = value => {
    const isoValue = moment(value, format).toISOString()
    this.props.onValueChange(isoValue)
  }
}