Android date value before 1900 or after 2038 crashes app
Bug report
Summary
On android in date mode with an old minimumDate (in 1800s), when a value is provided that is before Jan 1 1900 (yet past the provided minimumDate so it should be allowed) the app crashes.
Environment info
react-native info output:
System: OS: Windows 10 10.0.18362 CPU: (8) x64 Intel(R) Core(TM) i7-8650U CPU @ 1.90GHz Memory: 15.93 GB / 31.84 GB Binaries: Node: 10.15.1 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.0 - ~\AppData\Roaming\npm\yarn.CMD npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: API Levels: 24, 26, 28 Build Tools: 30.0.0 System Images: android-28 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom Android NDK: Not Found IDEs: Android Studio: Version 4.0.0.0 AI-193.6911.18.40.6514223 Languages: Java: 1.8.0_222 Python: 2.7.16 npmPackages: @react-native-community/cli: Not Found react: 16.11.0 => 16.11.0 react-native: https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz => 0.62.2 npmGlobalPackages: *react-native*: Not Found
Library version: 2.6.0 (The same behavior was on 2.4.0 and I updated just to make sure it wasn't fixed)
Steps to reproduce
- Using the provided code in an app, press the button to open the picker
- Select a date before Jan 1, 1900
- Press OK. The app crashes.
Note: This happens with dates in the far future too. When a parent component or context handles the state holding the date value and is updated via onChange, it works better but still has problems. It doesn't crash. The button has the right date. But when pressing the button again to open the picker again the top part in blue has the right date but the white part showing the month's days does NOT have the right date. It is at Jan 1 1900.
Describe what you expected to happen:
- The onChange callback provides the new selectedDate
- The button is updated with the new date
- When opening the picker again, the correct old date is shown
Reproducible sample code
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { StyleSheet, View, Button, Text } from 'react-native'
import DateTimePicker from '@react-native-community/datetimepicker'
const defaultStartingDate = new Date();
// Uncomment this to make it crash immediately
// defaultStartingDate.setFullYear(1850);
function EventDateTimePickerAndroid(props) {
const [show, setShow] = useState(false);
const [theDate, setTheDate] = useState(defaultStartingDate);
const onChange = (event, selectedDate) => {
setShow(false);
if (selectedDate) {
setTheDate(selectedDate);
}
};
const showDatePicker = () => {
setShow(true);
};
const theMaxDate = new Date();
theMaxDate.setFullYear(2300);
const theMinDate = new Date();
theMinDate.setFullYear(1800);
return (
<React.Fragment>
<View >
<Button onPress={showDatePicker} title={theDate.toLocaleDateString()} />
</View>
{show && (
<DateTimePicker
testID="dateTimePicker"
maximumDate={theMaxDate}
minimumDate={theMinDate}
value={theDate}
mode="date"
display="default"
onChange={onChange}
/>
)}
</React.Fragment>
);
};
export default EventDateTimePickerAndroid;
Here's a snack to reproduce. Open the datetimepicker and pick a date before 1900 or after 2038 to crash it. Or, uncomment this line to make it crash immediately: defaultStartingDate.setFullYear(1850);
Here is the reason why you can not select dates before 1900 and after 2038. Hope that makes sense