i used DateTimePicker in react-native I want to show Selected Date

Viewed 385

I am new to react native. I want to show My selected date it show in console. now in below code of dateTimePicker .what do you think the best solution please help. thanks

import React, { Component } from "react";
import DateTimePicker from "react-native-modal-datetime-picker";
export default class booking extends React.Component {
showDateTimePicker = () => {
    this.setState({ isDateTimePickerVisible: true });
  };
 
  hideDateTimePicker = () => {
    this.setState({ isDateTimePickerVisible: false });
  };
 
  handleDatePicked = date => {
    console.warn("A date has been picked: ", date);
   
   
    this.hideDateTimePicker();
  };
}
 render(){
       
        return (
          <View style={styles.container}>
              
              <Text style={styles.selectDate}>Select Date:</Text>
            <Button title="click:" onPress={this.showDateTimePicker} />
            <Text>ateTime: {String(this.state.date)}</Text>
           
        <DateTimePicker
   
         maximumDate={new Date(2022, 10, 20)} 
         minimumDate={new Date(2021, 5, 2)}
          isVisible={this.state.isDateTimePickerVisible}
          mode="date"
          onChange={ date => this.setState({ date }) }
          onConfirm={this.showDateTimePicker}
          onCancel={this.hideDateTimePicker}
          

         
        />
}
1 Answers

You were not using DateTimePicker properly. The simplest way to implement the DateTimePicker is to create a function that will trigger the Calendar or the Clock inside an input field when user click on the button or Text.

<TextInput
    placeholder=""
    onFocus={()=>RenderStartClock()}
/>

The function may be as below:

const RenderStartClock = () => {
         <DateTimePicker
               testID="dateTimePicker"
               value={startTime}
               mode={timeMode}
               is24Hour={true}
               display="clock"
               onChange={onChangeStartTime}
                                         />
                                  }
Related