反应本机日期选择器仅在调试模式下工作 [英] react native datepicker works only in debug mode
问题描述
我在 React Native 中使用 android 的本机日期选择器app ,只有当我使用调试模式时,我才会得到日期,但是当我使用真实手机取消调试模式时,我会在我选择调试的同一日期收到警报 'invalid date'.
I'm using native datepicker of android in react native app , only when I use debug mode I get date, but when I cancel debug mode using real phone I get in alert 'invalid date' in same date I choose with the debug.
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);
}
}
}
推荐答案
今天我遇到了类似的问题(并解决了!).
Today I had a similar issue (and solved it!).
我试图生成一个给定字符串的日期对象(德国日期格式)new Date("06.06.2020")
.之后,我尝试将生成的日期与今天的 new Date()
进行比较.
I've tried to generate an Date-Object of an given String (German Date Format) new Date("06.06.2020")
. After that, I've tried to compare the generated Date with today new Date()
.
由于它在调试模式下运行良好,因此不适用于捆绑的 Release APK.我和你一样收到了无效日期
-消息.
As it worked well on Debug-Mode, it doesn't work on a bundled Release APK. I got invalid Date
-Message even like you.
解决方案
我发现,您必须拥有完全合格的 ISO_8601 格式化字符串以生成日期.
I've figured out, that you must have an full qualified ISO_8601 formatted string to generate an date.
因此,我执行以下操作以将日期转换为 ISO_8601 格式:
So I did the following to convert my date in that ISO_8601 formatted:
const startNextFestival = "06.10.2020".split('.').reverse().join('-')+"T10:00:00.000Z";
const nextFestival = new Date(startNextFestival);
这会为 startNextFestival 生成以下日期字符串:2020-06-06T10:00:00.000Z"这样它就像一个魅力,生成.
This generates following Date-String for startNextFestival: "2020-06-06T10:00:00.000Z" This way it works like a charm, to generate.
希望这对某人有所帮助.
Hope that's helps somebody.
这篇关于反应本机日期选择器仅在调试模式下工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!