Datetimepicker: Cannot Set Initial Date

Created on 4 Feb 2020  路  12Comments  路  Source: react-native-datetimepicker/datetimepicker

@vonovak It seems that the date picker cannot be set to an initial date. It always opens to todays date and ignores the value passed to value. Is this the intended behavior?

Thanks in advance for any help you might be able to provide

All 12 comments

hi and thanks for asking. Can you provide a code snippet? What platform is this on? thanks.

for me it works with ios but on android the datetimepicker always sets the date to the current date

hey guys, I noticed this before and seeing the code I have a question

why a new Date() is used here instead of using pre-defined value?

// @react-native-community/datetimepicker/src/datetimepicker.android.js
picker.then(
    function resolve({action, day, month, year, minute, hour}) {
      const date = new Date(); <-- here 馃槙 
      ...
  );

I started the component with value eg. 2020-02-13 but when onChange event is triggered returns 2020-02-12, happening on Android with timepicker only.

I have the same problem

@jackfiallos use moment..
moment.utc(date).local().format('MM/DD/YYYY')
and its work for me

@jackfiallos use moment..
moment.utc(date).local().format('MM/DD/YYYY')
and its work for me

Hi @salmanitb, I think that's not gonna fix the problem because is not a formatting issue, in fact the issue is with the date object.

Anyone can confirm this? or it's me doing something silly.

Hello @jackfiallos, faced the same issue on android :(
No matter what date was selected, if I as a user open date time picker modal I see current date

"@react-native-community/datetimepicker": "^2.2.2",
"react-native": "0.61.5",

The reason was in missing updates. Solved by adding

useEffect(() => {
   if (!isIOS) {
     setSelectedDate(date)
   }
}, [date])

to my component DatePickerModal which renders DateTimePicker and receives date as a prop

Hope it can help you as well @jackfiallos

Thanks @nataly-chobat, I've solve this differently but gonna update and see how the new patch works, I saw the code changed after my suggestion.

I have the same problem... probably it's not fixed yet. I am using mmazzarolo/react-native-modal-datetime-picker which uses this library. Do you guys have some other solutions?

This should be fixed by https://github.com/react-native-community/datetimepicker/commit/1871168d1bb4073b42455e9a03188654d7234321

If you're still experiencing this, please open a new issue and follow the issue template, thanks!

Was this page helpful?
0 / 5 - 0 ratings