本文介绍了从 ReactNative-DatePickerAndroid 获取所选日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注 ReactNative DatePickerAndroid 文档.对于 DatePickerIOS,我们有 onDateChange方法.

I am following ReactNative DatePickerAndroid Docs.For DatePickerIOS we have onDateChange method.

有没有类似的方法可以从 DatePickerAndroid 中获取选定的日期?上面的文档没有提到任何获取选定日期的代码示例.

Is there any similar method to get selected date from DatePickerAndroid? Above documentation didn't mention any code example to get selected date.

推荐答案

您可以从给定的代码示例中获取 showPicker 方法中的选定日期:

You can get selected date in showPicker method from given code example :

showPicker = async (stateKey, options) => {
    try {
      var newState = {};
      const {action, year, month, day} = await DatePickerAndroid.open(options);
      if (action === DatePickerAndroid.dismissedAction) {
        newState[stateKey + 'Text'] = 'dismissed';
      } else {
        // <<<< Newly selected date >>>>
        var date = new Date(year, month, day);
        newState[stateKey + 'Text'] = date.toLocaleDateString();
        newState[stateKey + 'Date'] = date;
      }
      this.setState(newState);
    } catch ({code, message}) {
      console.warn(`Error in example '${stateKey}': `, message);
    }
  };
render() {
   return (
   <TouchableWithoutFeedback
    onPress={this.showPicker.bind(this, 'spinner', { date: this.state.presetDate })}>
     <View>
        <Text style={styles.text}>Date selector</Text>
     </View>
   </TouchableWithoutFeedback>
  )
}

这篇关于从 ReactNative-DatePickerAndroid 获取所选日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 08:43