简体   繁体   English

如何在 React Native 中显示选定的时间

[英]How can I show the selected time in React Native

First of all, hello everyone.首先,大家好。 This is my first question.这是我的第一个问题。

I'm trying to use time picker in React Native.我正在尝试在 React Native 中使用时间选择器。 I installed the package from the repo on GitHub.我从 GitHub 上的 repo 安装了这个包。 But I couldn't display the time in the text component?但是我无法在文本组件中显示时间? How can I do?我能怎么做?

const Example = (props) => {
  
    const [isDatePickerVisible, setDatePickerVisibility] = useState(false);

 
    const showDatePicker = () => {
      setDatePickerVisibility(true);
  };

    const hideDatePicker = () => {
      setDatePickerVisibility(false);
  };

    const handleConfirm = (date) => {
       console.warn("A date: ", date);
       settimeShow(date)

       hideDatePicker();
  };

  return (
    <View>
       <Button title="Show Time Picker" onPress={showDatePicker} />
       <Text>
         ?
       </Text>

    <DateTimePickerModal
      isVisible={isDatePickerVisible}
      mode="time"
      onConfirm={handleConfirm}
      onCancel={hideDatePicker}
    />

  </View>
  );
};

enter image description here在此处输入图片说明

https://github.com/mmazzarolo/react-native-modal-datetime-picker https://github.com/mmazzarolo/react-native-modal-datetime-picker

your can get Date text in parameter date .您可以在参数date获取日期文本。 in handleConfirm save date in one state and show it in Text :)handleConfirm在一种state保存日期并在文本中显示:)

import React, { Component } from "react";
import { Button, View } from "react-native";
import DateTimePicker from "react-native-modal-datetime-picker";
 
export default class DateTimePickerTester extends Component {
  constructor(props) {
    super(props);
    this.state = {
      isDateTimePickerVisible: false,
      selectedDate:'',
    };
  }
 
  showDateTimePicker = () => {
    this.setState({ isDateTimePickerVisible: true });
  };
 
  hideDateTimePicker = () => {
    this.setState({ isDateTimePickerVisible: false });
  };
 
  handleDatePicked = date => {
    console.log("A date has been picked: ", date);
    setstate({selectedDate:date});
    this.hideDateTimePicker();
  };
 
  render() {
    return (
      <>
        <Button title="Show DatePicker" onPress={this.showDateTimePicker} />
        <Text>this.state.selectedDate</Text>
        <DateTimePicker
          isVisible={this.state.isDateTimePickerVisible}
          onConfirm={this.handleDatePicked}
          onCancel={this.hideDateTimePicker}
        />
      </>
    );
  }
}

您可以使用moment来格式化您的时间,然后显示它,

moment(date).format('LT') /// example : 8:30 PM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在反应原生 dateTimePicker 中显示选定的日期和时间 - I want to show Selected Date and time in react native dateTimePicker 如何在反应本机的文本输入中显示选定的日期和时间? - How to show selected date and time in textinput in react native? 如何在反应本机的文本输入中显示选定的日期和时间 - How to show selected date and time in textinput in react native 我想在本机反应中显示选定的数字 - i want to show selected numbers in react native 如何在React Native中显示不同的所选项目 - How to show selected item different in React Native 每次我选择 React Native datepicker 时,如何从它获取选定的日、月、年和小时、分钟? - How can i get selected day, month, year and hours,minute from React Native datepicker every time i pick it? 选择时 React Native TextInput 缩小,我该如何停止它? - React Native TextInput shrinks when selected, how can I stop it? 如何在 React-Native 中删除手风琴中的选定部分 - How Can I delete selected section in Accordion in React-Native 如何在 React Native 的平面列表中更改所选项目的颜色? - How can I change the color of selected item in flatlist in React Native? 如何在 React Native 中使用 require() 显示动态图像? - How can I show dynamic images with require() in React Native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM