【问题标题】:How can i get selected day, month, year and hours,minute from React Native datepicker every time i pick it?每次我选择 React Native 日期选择器时,如何从 React Native 日期选择器中获取选定的日期、月份、年份和小时、分钟?
【发布时间】:2019-08-08 14:00:42
【问题描述】:
import DatePicker from 'react-native-datepicker';

export default class App extends Component {
   static navigationOptions= ({navigation}) =>({
          header: null
    });
constructor(props) {
    super(props);

    this.state = {
      date: '',
      time: '',

    };
  }

  componentWillMount() {
    this._panResponder = PanResponder.create({
      onStartShouldSetPanResponder: (e) => {console.log('onStartShouldSetPanResponder'); return true;},
      onMoveShouldSetPanResponder: (e) => {console.log('onMoveShouldSetPanResponder'); return true;},
      onPanResponderGrant: (e) => console.log('onPanResponderGrant'),
      onPanResponderMove: (e) => console.log('onPanResponderMove'),
      onPanResponderRelease: (e) => console.log('onPanResponderRelease'),
      onPanResponderTerminate: (e) => console.log('onPanResponderTerminate')
    });


  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to react-native-datepicker example!
        </Text>
        <DatePicker
          style={{width: 200}}
          date={this.state.date}
          mode="date"
          placeholder="placeholder"
          format="DD-MM-YYYY"
          minDate="01-01-1930"
          maxDate="31-12-2099"
          confirmBtnText="Confirm"
          cancelBtnText="Cancel"
          iconSource={require('./google_calendar.png')}
          onDateChange={(date) => {this.setState({date: date});}}
        />
        <Text style={styles.instructions}>date: {this.state.date}</Text>
        <DatePicker
          style={{width: 200}}
          date={this.state.time}
          mode="time"
          placeholder="placeholder"
          format="HH:mm"
          confirmBtnText="Confirm"
          cancelBtnText="Cancel"
          minuteInterval={10}
          onDateChange={(time) => {this.setState({time: time});}}
        />
        <Text style={styles.instructions}>time: {this.state.time}</Text>
      </View>
    );
  }
}

我如何从这些数据中获取日、月、年、小时和分钟?

【问题讨论】:

  • 谁能帮帮我?
  • 你在this.state.date 变量中得到了什么???
  • this.state = { date:''} 选择的日期类似于 01-11-1933

标签: mysql reactjs react-native datetime


【解决方案1】:

您可以使用不同的内置函数来提取日、月、年。

例如

getDateAttributes(){
    let d = new Date(this.state.date);  // i assume your date as 01-11-1933
    d.getDate(); // 11
    d.getMonth(); // 0  month is like array so you have to do +1 for correct month
    d.getFullYear(); // 1933
}

【讨论】:

  • 你能帮帮我吗?我怎样才能运行这个功能
  • 我怎样才能得到小时和分钟?在日期时间
  • 选择日期后
【解决方案2】:

只需替换mode='date', 与:mode='datetime' 在您的 DatePicker 组件中。

查看props了解更多信息

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2011-05-04
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多