Ruchira Swarnapriya
Ruchira Swarnapriya

Reputation: 1015

React Native Community Date Picker : Set Picked Date to Text Component (React State Hooks)

I have implemented to pick and view the date as following ,

const [pickDate, setPickDate] = useState(new Date());

const onChange = (event, selectedDate) => {
     const currentDate = selectedDate || date;
     setShow(Platform.OS === 'ios');
     setDate(currentDate);
     setPickDate(selectedDate);
   };

<View> 
    <View>
       <Button onPress={showDatepicker} title="Show date picker!" />
       </View>
       <View>
         <Button onPress={showTimepicker} title="Show time picker!" />
       </View>
       {show && (
         <DateTimePicker
           testID="dateTimePicker"
           value={date}
           mode={mode}
           is24Hour={true}
           display="default"
           onChange={onChange}
         />
       )}
       <Text>{pickDate}</Text>
    </View>
</View>

But I have got the following error when after the pick the date.

enter image description here

Can anyone suggest the solution ?

Upvotes: 3

Views: 569

Answers (1)

shubham
shubham

Reputation: 1319

As per the official documentation, the date should be in the string format.

Like "2016-05-15", what you seem to pass a Date object. const [pickDate, setPickDate] = useState(new Date());

Kindly replace date object with the string, that will surely work.

Ref: https://github.com/xgfe/react-native-datepicker

Upvotes: 1

Related Questions