0

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);
        }
    }

}
Manspof
  • 598
  • 26
  • 81
  • 173
  • 1
    you can change this string `${year} ${month} ${day}` to `${year}/${month}/${day}` so it will not say 'invalid date' again – bun houth Jul 08 '18 at 18:31
  • thanks. it solve my issue, why in my way it's not works? – Manspof Jul 08 '18 at 19:19
  • 1
    I think for android when the format doesn't have dash or slash it doesn't work. However, when the format is set to "YY-MM-DD" or "YY/MM/DD", it will work. – Sandy..... Jul 08 '18 at 19:33
  • okay fine. good to know. thanks for your time – Manspof Jul 08 '18 at 19:37
  • could you please help me with this issue? https://stackoverflow.com/questions/61967235/react-native-moment-not-working-for-specific-time-value-without-debug-mode – newdeveloper May 23 '20 at 05:36

1 Answers1

0

Today I had a similar issue (and solved it!).

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().

As it worked well on Debug-Mode, it doesn't work on a bundled Release APK. I got invalid Date-Message even like you.

Solution

I've figured out, that you must have an full qualified ISO_8601 formatted string to generate an date.

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);

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.

suther
  • 12,600
  • 4
  • 62
  • 99