Skip to content

example中的时间选择,ios中即使日期改变为2011-02-28,再点击onPickerConfirm后还是会返回2011-02-31 #328

@shenshenwlb

Description

@shenshenwlb

showTimePicker = () => {
const years = []
const time = timeGroup

for (let i = 1; i < 30; i++) {
  years.push(i + 2010 + '年')
}

let pickerData = []
let selectedValue = []
const date = new Date()

if (this.props.showTime) {
  pickerData = [years, monthGroup, dayGroup, time]
  selectedValue = [
    date.getFullYear(),
    date.getMonth() + 1,
    date.getDate(),
    date.getTime()
  ]
} else {
  pickerData = [years, monthGroup, dayGroup]
  selectedValue = [
    date.getFullYear(),
    date.getMonth() + 1,
    date.getDate()
  ]
}
Picker.init({
  pickerData,
  selectedValue,
  pickerConfirmBtnText: '确定',
  pickerCancelBtnText: '取消',
  pickerToolBarBg: backGroundColor,
  pickerBg: backGroundColor,
  pickerTitleText: this.props.title,
  pickerTextEllipsisLen: 20,
  onPickerConfirm: pickedValue => {
    console.log('xxx pickedValue',pickedValue)
    this.props.onRequestClose()
    this.props.onRequestSave(pickedValue)
  },
  onPickerCancel: pickedValue => {
    this.props.onRequestClose()
  },
  onPickerSelect: pickedValue => {
    const targetValue = [...pickedValue]
    if (Number.parseInt(targetValue[1]) === 2) {
      if (Number.parseInt(targetValue[0]) % 4 === 0 && Number.parseInt(targetValue[2]) > 29) {
        targetValue[2] = 29 + '日'
      } else if (Number.parseInt(targetValue[0]) % 4 !== 0 && Number.parseInt(targetValue[2]) > 28) {
        targetValue[2] = 28 + '日'
      }
    } else if (Number.parseInt(targetValue[1]) in { 4: 1, 6: 1, 9: 1, 11: 1 } && Number.parseInt(targetValue[2]) > 30) {
      targetValue[2] = 30 + '日'

    }
    // forbidden some value such as some 2.29, 4.31, 6.31...
    if (JSON.stringify(targetValue) !== JSON.stringify(pickedValue)) {
      // android will return String all the time,but we put Number into picker at first
      // so we need to convert them to Number again
      targetValue.map((v, k) => {
        if (k !== 3) {
          targetValue[k] = v
        }
      })
      Picker.select(targetValue)
      pickedValue = targetValue
      console.log('xxx pickedValue',pickedValue)
    }
  }
})
Picker.show()

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions