React Native DateTimePicker

repository·master·Indexed 25 days ago

https://github.com/react-native-datetimepicker/datetimepicker

A date and time picker component for React Native supporting iOS, Android, and Windows. It provides a declarative component API and a recommended imperative API (DateTimePickerAndroid) for Android. Features include support for various display styles (spinner, compact, inline, calendar, clock), Material 3 design on Android, time zone configuration, and localization support across platforms.

Tokens
8.5K
Snippets
22
Records
49
Agent score
83%

What's inside @react-native-community/datetimepicker

  1. Migrate from DatePickerAndroid to RNDateTimePicker

    master

    When migrating from the legacy DatePickerAndroid imperative API to the RNDateTimePicker component:

    • Replace date with the value prop.
    • Replace minDate and maxDate with minimumDate and maximumDate.
    • Replace imperative action checks (dateSetAction and dismissedAction) with the onChange callback:
      • A selection (formerly dateSetAction) is indicated when the date argument in onChange is defined.
      • A dismissal (formerly dismissedAction) is indicated when the date argument in onChange is undefined.
  2. Configure Android styling for DateTimePicker

    master

    To customize the appearance of the Android Date and Time pickers, you must use version 8.2.0 or higher and an Expo Development build.

    Configuration is handled via the @react-native-community/datetimepicker plugin in your app.json or app.config.js.

    Important Rules:

    • You cannot specify a color for dark mode only. To influence dark mode, you must provide values for both light and dark keys. If one is missing, the plugin will throw an error.
    • The plugin validates that the property names (e.g., textColorPrimary) are valid.

    After modifying your configuration, you must regenerate native code and rebuild to see changes:

    npx expo prebuild -p android --clean
    expo run:android
  3. Manually install on iOS

    master

    To manually install @react-native-community/datetimepicker on iOS, follow these steps:

    1. Install CocoaPods.
    2. Run pod init inside your ios folder.
    3. Update your Podfile with the configuration below (replace MyApp with your actual target name).
    4. Note: Because the library uses Swift, you must ensure use_frameworks! is enabled.
    5. Run pod install in the ios folder.
    6. Start your project using npm run start and npm run start:ios.

    Important Configuration Details:

    • Set platform :ios, '8.0' or higher.
    • Point the RNDateTimePicker pod to the path in node_modules.
    • If using React Native >= 0.42.0, explicitly include yoga.
    • Include necessary React subspecs (Core, CxxBridge, DevSupport, RCTText, RCTNetwork, RCTWebSocket).
    # Allowed sources
    source 'https://github.com/CocoaPods/Specs.git'
    
    target 'MyApp' do
      # As we use Swift, ensure that `use_frameworks` is enabled.
      use_frameworks!
    
      # Specific iOS platform we are targetting
      platform :ios, '8.0'
    
      # Point to the installed version
      pod 'RNDateTimePicker', :path => '../node_modules/@react-native-community/datetimepicker/RNDateTimePicker.podspec'
    
      # React/React-Native specific pods
      pod 'React', :path => '../node_modules/react-native', :subspecs => [
        'Core',
        'CxxBridge',      # Include this for RN >= 0.47
        'DevSupport',     # Include this to enable In-App Devmenu if RN >= 0.43
        'RCTText',
        'RCTNetwork',
        'RCTWebSocket',   # Needed for debugging
      ]
    
      # Explicitly include Yoga if you are using RN >= 0.42.0
      pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga'
    
      # Third party deps podspec link
      pod 'DoubleConversion', :podspec => '../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec'
      pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec'
      pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec'
    end
  4. Configure Android styling in app.json / app.config.js

    master

    Add the @react-native-community/datetimepicker plugin to your Expo configuration to apply custom colors to the Android pickers. Use the android.datePicker and android.timePicker objects to define styles for light and dark modes.

    {
      "expo": {
        "plugins": [
          [
            "@react-native-community/datetimepicker",
            {
              "android": {
                "datePicker": {
                  "colorAccent": {
                    "light": "#FF5722"
                  },
                  "textColorPrimary": {
                    "light": "#FF5722"
                  }
                },
                "timePicker": {
                  "background": {"light": "#FF5722", "dark": "#383838"},
                  "numbersBackgroundColor": {"light": "#FF5722", "dark": "#383838"}
                }
              }
            }
          ]
        ]
      }
    }
  5. Run the example app

    master

    To run the provided example project to see the library in action or to perform development, follow these steps in order:

    1. Run yarn in the repository root.
    2. Navigate to the example directory: cd example.
    3. Install required pods: npx pod-install.
    4. Start the Metro Bundler: yarn start.
    5. Run the application for your target platform using yarn run start:ios, yarn run start:android, or yarn run start:windows.

    Note for Developers: To develop on the library itself, open the project located in the example folder using Xcode or Android Studio. Changes made to the library code will be reflected in the example project.

    yarn
    cd example
    npx pod-install
    yarn start
    yarn run start:ios
  6. Install @react-native-community/datetimepicker

    master

    Install the package using npm or yarn.

    iOS Setup: After installation, you must run npx pod-install and then rebuild your project.

    Windows Setup: Autolinking is not implemented for Windows; manual installation is required.

    npm install @react-native-community/datetimepicker --save
    
    # or
    
    yarn add @react-native-community/datetimepicker
  7. Install for Expo users

    master

    If you are using Expo Go, use npx expo install to ensure you get the version compatible with your Expo SDK.

    If you are using a Dev Client, rebuild the Dev Client after installing. If you use expo prebuild, you can use the latest version of the module.

    npx expo install @react-native-community/datetimepicker
  8. Handle localization for DateTimePicker

    master

    Localization (month/day names, date order, and 12/24 hour format) is handled differently depending on the platform:

    • Android: The picker is controlled by the system locale. To change it, follow the Android developer instructions.
    • iOS: The picker automatically displays localized strings if the target language is included in your project.pbxproj via Xcode. You do not need to provide manual translation keys for days or months. For Expo users, follow the Expo localization documentation.

    Note on iOS locale prop: There is an iOS-only locale prop that can force a locale, but its usage is discouraged because it is not robust in all picker modes (e.g., it may cause mixed month and day names). It is reported to work reliably only in spinner mode.

  9. Manually install on Windows

    master

    To manually install the DateTimePicker for Windows, follow these steps in Visual Studio 2019:

    1. Add Project: Right-click your solution in Solution Explorer > Add > Existing Project. Select the .vcxproj file located at: node_modules/@react-native-community/datetimepicker/windows/DateTimePickerWindows/DateTimePickerWindows.vcxproj.
    2. Add Reference: Right-click your main application project > Add > Reference... and check DateTimePickerWindows under the 'Project > Solution' tab.
    3. Update pch.h: Add #include "winrt/DateTimePicker.h".
    4. Update app.cpp: Add PackageProviders().Append(winrt::DateTimePicker::ReactPackageProvider()); before the InitializeComponent(); call.
  10. Migrate from TimePickerAndroid to RNDateTimePicker

    master

    When migrating from the legacy TimePickerAndroid imperative API to the RNDateTimePicker component:

    • Replace hour and minute props with a single value prop (the component uses the hour and minute defined in the provided Date object).
    • Replace imperative action checks (timeSetAction and dismissedAction) with the onChange callback:
      • A selection (formerly timeSetAction) is indicated when the date argument in onChange is defined.
      • A dismissal (formerly dismissedAction) is indicated when the date argument in onChange is undefined.
  11. Manually install on Android

    master

    To manually install @react-native-community/datetimepicker on Android, perform the following three steps:

    1. Modify android/settings.gradle: Include the project and point it to the node_modules directory.
    2. Modify android/app/build.gradle: Add the project to your dependencies.
    3. Modify MainApplication.java: Import the package and manually add it to the list of packages in getPackages() (required if autolinking is not working).
    # 1. android/settings.gradle
    include ':@react-native-community_datetimepicker'
    project(':@react-native-community_datetimepicker').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/datetimepicker/android')
    
    # 2. android/app/build.gradle
    dependencies {
        ...
        implementation project(':@react-native-community_datetimepicker')
    }
    
    # 3. MainApplication.java
    import com.reactcommunity.rndatetimepicker.RNDateTimePickerPackage;
    
    // ... inside getPackages()
    packages.add(new RNDateTimePickerPackage());