react-native-datetimepicker / datetimepicker

React Native date & time picker component for iOS, Android and Windows
MIT License
2.54k stars 413 forks source link

Fixed Issue #935: Unable to select a date earlier than Unix epoch whe… #938

Closed FiratDede closed 1 week ago

FiratDede commented 3 weeks ago

…n only a maximum date is provided on Android

Summary

The error was described in https://github.com/react-native-datetimepicker/datetimepicker/issues/935

This pull request solves https://github.com/react-native-datetimepicker/datetimepicker/issues/935 . According to old code when you provide only maximum date, it gets minimum date timestamp as 0 (which is Thursday, January 1, 1970 12:00:00 AM) . As a result when you select the date before the minimum date, it selects minimum date, I am not sure whether it is a bug because I think you shouldn't select a date before minimum date if you give a value to maximum date . However, Maybe used minimumDate value can be changed here because there is a constant which is DEFAULT_MIN_DATE timestamp equals to -2208988800001l in RNConstants.java. You can see the code below in RNConstants.java

  /**
   * Minimum date supported by {@link TimePickerDialog}, 01 Jan 1900
   */
  public static final long DEFAULT_MIN_DATE = -2208988800001l;

And this constants matches 01 Jan 1990 date. My pull request only changes minDate as 01 Jan 1900, if this minDate is not provided by the user.

Test Plan

What's required for testing (prerequisites)?

You can use your physical devices or emulators for testing.

What are the steps to reproduce (after prerequisites)?

You can use the js code below for seeing the bug. You can replace example/App.js code with the code below:

import { useState } from "react";
import DateTimePicker from "@react-native-community/datetimepicker";
import { View, Button } from "react-native";

export default function App() {
  const [show, setShow] = useState(false);
  const [value, setValue] = useState(new Date(1970, 0, 2))

  return (
    <View style={{
      flex: 1,
      padding: 42
    }}>
      <Button title="Select Date" onPress={(e) => {
        setShow(true)
      }} />

      {show && (
        <DateTimePicker
          maximumDate={new Date(2006, 0, 1)}
          minimumDate={undefined}
          value={value}
          mode={"date"}
          onChange={
            (e, selectedDate) => {
              setValue(selectedDate)
              setShow(false)
            }}
        />)}
    </View>
  );
}

Some Videos For Showing Changes

Before Changes: https://github.com/user-attachments/assets/42a6f688-37c1-4b1d-9f17-4e84b285f35b

After Changes: https://github.com/user-attachments/assets/673fb04f-9e8f-4f52-942e-c7af065d1d59

Compatibility

OS Implemented
iOS
Android

Checklist

FiratDede commented 1 week ago

Actually this PR doesn't solve #935 issue completely. As a result I will close this PR.