import React, { Component } from 'react' import { StyleSheet, View, Text, Dimensions, TouchableOpacity, Platform, DatePickerAndroid, } from 'react-native' import { connect } from 'react-redux' import Collapsible from 'react-native-collapsible' import SelectTime from '../../containers/common/SelectTime' import { setNowFormatDate } from '../../utils/utils' import moment from 'moment' const { width, height } = Dimensions.get('window') @connect(({ theme }) => ({ ...theme, })) class SelectTimeCom extends Component { constructor(props) { const date = new Date() super(props) this.state = { // 开始时间 startTime: setNowFormatDate(true), // date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(), // 结束时间 endTime: setNowFormatDate(), // date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() } } async TimeAndroid(mark) { const { action, year, month, day } = await DatePickerAndroid.open({ date: new Date(), // minDate: new Date(), mode: 'spinner', }) if (action !== DatePickerAndroid.dismissedAction) { let newDate = `${year}-${(month + 1)<10?"0"+month:month}-${day}` newDate = moment(newDate, 'YYYY-MM-DD').format('YYYY-MM-DD') if (mark == 'start') { if (newDate <= this.state.endTime) { this.setState({ startTime: newDate, }) } } else if (mark == 'end') { if (this.state.startTime <= newDate) { this.setState({ endTime: newDate, }) } } // 这里开始可以处理用户选好的年月日三个参数:year, month (0-11), day } } render() { return ( 开始时间 Platform.OS == 'ios' ? this.showTime._openModal('start') : this.TimeAndroid('start') } style={{ paddingHorizontal: 40, paddingVertical: 7, marginTop: 16, borderColor: '#DDD', borderWidth: 1, borderRadius: 100, }} > {this.state.startTime} ~ 结束时间 Platform.OS == 'ios' ? this.showTime._openModal('end') : this.TimeAndroid('end') } style={{ paddingHorizontal: 40, paddingVertical: 7, marginTop: 16, borderColor: '#DDD', borderWidth: 1, borderRadius: 100, }} > {this.state.endTime} { data = { fn: 'cancel' } this.props.dateCb(data) }} style={{ width: width / 2, backgroundColor: '#FFF', justifyContent: 'center', }} > 取消 { data = { startTime: this.state.startTime, endTime: this.state.endTime, } this.props.dateCb(data) }} style={{ width: width / 2, backgroundColor: '#E70013', justifyContent: 'center', }} > 确定 {/* 时间弹窗 */} (this.showTime = showTime)} cb={(date, mark) => { let newDate = moment(date).format('YYYY-MM-DD') if (mark == 'start') { if (newDate <= this.state.endTime) { this.setState({ startTime: newDate, }) } } else if (mark == 'end') { if (this.state.startTime <= newDate) { this.setState({ endTime: newDate, }) } } }} /> ) } } const styles = StyleSheet.create({ container: { flex: 1, }, footer: { flexDirection: 'row', height: 24, justifyContent: 'center', alignItems: 'center', marginBottom: 10, marginTop: 10, }, }) export default SelectTimeCom