【发布时间】:2018-07-08 18:22:15
【问题描述】:
我在 react native app 中使用native datepicker of android,只有当我使用调试模式时我才会得到日期,但是当我使用真机取消调试模式时,我会收到警报 'invalid date'我选择调试的同一日期。
showDatePicker = async () => {
if (Platform.OS === 'ios') {
this.setState({ datePickerIosPlatform: true })
} else {
try {
const { action, year, month, day } = await DatePickerAndroid.open({
date: new Date()
});
if (action !== DatePickerAndroid.dismissedAction) {
console.log('action', action, 'year', year, ' month', month, 'day', day)
var stringDate = `${year} ${month} ${day}`;
var newDate = new Date(stringDate)
alert(newDate)
var convertDate = moment(newDate).format('DD.MM.YYYY')
console.log('convertdata',convertDate)
// this.convertDate(stringDate)
}
} catch ({ code, message }) {
console.warn('Cannot open date picker', message);
}
}
}
【问题讨论】:
-
您可以将此字符串
${year} ${month} ${day}更改为${year}/${month}/${day},这样它就不会再显示“无效日期”了 -
谢谢。它解决了我的问题,为什么以我的方式不起作用?
-
我认为对于 android,当格式没有破折号或斜线时它不起作用。但是,当格式设置为“YY-MM-DD”或“YY/MM/DD”时,它会起作用。
-
好吧好吧。很高兴知道。感谢您的宝贵时间
-
您能帮我解决这个问题吗? stackoverflow.com/questions/61967235/…
标签: react-native