Convert Figma logo to code with AI

mmazzarolo logoreact-native-modal-datetime-picker

A React-Native datetime-picker for Android and iOS

2,951
395
2,951
38

Top Related Projects

React Native date & time picker component for iOS, Android and Windows

React Native Date Picker is datetime picker for Android and iOS. It includes date, time and datetime picker modes. The datepicker is customizable and is supporting different languages. It's written with native code to achieve the best possible look, feel and performance.

React Native Calendar Components 🗓️ 📆

react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS

1,486

Picker is a cross-platform UI component for selecting an item from a list of options.

🔽 A Picker component for React Native which emulates the native <select> interfaces for iOS and Android

Quick Overview

React Native Modal Datetime Picker is a customizable, cross-platform date and time picker component for React Native applications. It provides a modal interface for selecting dates and times, with support for both iOS and Android platforms.

Pros

  • Cross-platform compatibility (iOS and Android)
  • Highly customizable appearance and behavior
  • Easy integration with React Native projects
  • Supports both date and time picking modes

Cons

  • Requires additional setup for Android
  • May have inconsistencies in appearance between platforms
  • Limited built-in styling options for some elements
  • Potential performance issues with large date ranges

Code Examples

  1. Basic usage:
import DateTimePicker from '@react-native-community/datetimepicker';
import { Button } from 'react-native';

const MyComponent = () => {
  const [isDatePickerVisible, setDatePickerVisibility] = useState(false);

  const showDatePicker = () => {
    setDatePickerVisibility(true);
  };

  const hideDatePicker = () => {
    setDatePickerVisibility(false);
  };

  const handleConfirm = (date) => {
    console.warn("A date has been picked: ", date);
    hideDatePicker();
  };

  return (
    <Button title="Show Date Picker" onPress={showDatePicker} />
    <DateTimePicker
      isVisible={isDatePickerVisible}
      mode="date"
      onConfirm={handleConfirm}
      onCancel={hideDatePicker}
    />
  );
};
  1. Customizing the appearance:
<DateTimePicker
  isVisible={isDatePickerVisible}
  mode="datetime"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
  minimumDate={new Date()}
  maximumDate={new Date(2023, 12, 31)}
  isDarkModeEnabled
  textColor="#FFFFFF"
  buttonTextColorIOS="#007AFF"
/>
  1. Using with a custom button:
import { TouchableOpacity, Text } from 'react-native';

<TouchableOpacity onPress={showDatePicker}>
  <Text>Custom Date Picker Button</Text>
</TouchableOpacity>
<DateTimePicker
  isVisible={isDatePickerVisible}
  mode="time"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

Getting Started

  1. Install the package:

    npm install react-native-modal-datetime-picker @react-native-community/datetimepicker
    
  2. Import the component:

    import DateTimePicker from 'react-native-modal-datetime-picker';
    
  3. Use the component in your React Native app:

    const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
    
    const showDatePicker = () => {
      setDatePickerVisibility(true);
    };
    
    const hideDatePicker = () => {
      setDatePickerVisibility(false);
    };
    
    const handleConfirm = (date) => {
      console.log("A date has been picked: ", date);
      hideDatePicker();
    };
    
    return (
      <DateTimePicker
        isVisible={isDatePickerVisible}
        mode="date"
        onConfirm={handleConfirm}
        onCancel={hideDatePicker}
      />
    );
    

Competitor Comparisons

React Native date & time picker component for iOS, Android and Windows

Pros of datetimepicker

  • Native implementation for both iOS and Android, providing a more platform-specific look and feel
  • Lightweight and performant, as it uses native components
  • Offers more customization options for appearance and behavior

Cons of datetimepicker

  • Requires more setup and configuration, especially for Android
  • Less consistent cross-platform appearance, which may not be ideal for some apps
  • Modal functionality needs to be implemented separately if desired

Code Comparison

react-native-modal-datetime-picker:

<DateTimePickerModal
  isVisible={isDatePickerVisible}
  mode="date"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

datetimepicker:

{showPicker && (
  <DateTimePicker
    value={date}
    mode="date"
    display="default"
    onChange={onChange}
  />
)}

The react-native-modal-datetime-picker provides a simpler API with built-in modal functionality, while datetimepicker requires manual handling of visibility and platform-specific implementations.

react-native-modal-datetime-picker offers a more streamlined, cross-platform consistent experience out of the box, making it easier to implement for developers who prioritize quick setup and uniform appearance across platforms. However, datetimepicker provides more native feel and potentially better performance, at the cost of additional setup and platform-specific considerations.

React Native Date Picker is datetime picker for Android and iOS. It includes date, time and datetime picker modes. The datepicker is customizable and is supporting different languages. It's written with native code to achieve the best possible look, feel and performance.

Pros of react-native-date-picker

  • Offers a native date picker for both iOS and Android
  • Supports a wide range of customization options, including custom styles and localization
  • Provides time-only and datetime picker modes in addition to date-only

Cons of react-native-date-picker

  • Requires manual modal implementation if a modal presentation is desired
  • May have a steeper learning curve due to more configuration options
  • Less focused on providing a pre-styled, ready-to-use modal solution

Code Comparison

react-native-modal-datetime-picker:

<DateTimePickerModal
  isVisible={isDatePickerVisible}
  mode="date"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

react-native-date-picker:

<DatePicker
  date={date}
  onDateChange={setDate}
  mode="date"
/>

The react-native-modal-datetime-picker provides a modal wrapper out of the box, while react-native-date-picker requires manual modal implementation if desired. react-native-date-picker offers more granular control over the picker's appearance and behavior, but may require more setup code for advanced use cases.

Both libraries are actively maintained and have good community support. The choice between them depends on whether you prefer a pre-packaged modal solution or more flexibility in implementation and styling.

React Native Calendar Components 🗓️ 📆

Pros of react-native-calendars

  • Offers a wide range of calendar components (day calendar, week calendar, agenda view)
  • Highly customizable with extensive theming options
  • Supports both iOS and Android platforms

Cons of react-native-calendars

  • Larger package size due to more features and components
  • Steeper learning curve for complex customizations
  • May require additional setup for certain advanced features

Code Comparison

react-native-calendars:

import {Calendar} from 'react-native-calendars';

<Calendar
  onDayPress={(day) => {console.log('selected day', day)}}
  markedDates={{
    '2023-05-16': {selected: true, marked: true, selectedColor: 'blue'}
  }}
/>

react-native-modal-datetime-picker:

import DateTimePickerModal from "react-native-modal-datetime-picker";

<DateTimePickerModal
  isVisible={isDatePickerVisible}
  mode="date"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

The react-native-calendars library provides a more comprehensive calendar view with built-in styling options, while react-native-modal-datetime-picker focuses on a modal-based date and time selection. The choice between the two depends on the specific requirements of your project, such as the need for a full calendar view versus a simple date/time picker.

react native datePicker component for both Android and IOS, useing DatePikcerAndroid, TimePickerAndroid and DatePickerIOS

Pros of react-native-datepicker

  • More customizable appearance with various style props
  • Supports both date and time selection in a single component
  • Offers a range of format options for displaying selected dates

Cons of react-native-datepicker

  • Less actively maintained, with fewer recent updates
  • May have compatibility issues with newer React Native versions
  • Limited modal functionality compared to react-native-modal-datetime-picker

Code Comparison

react-native-datepicker:

<DatePicker
  style={{width: 200}}
  date={this.state.date}
  mode="date"
  placeholder="Select date"
  format="YYYY-MM-DD"
  confirmBtnText="Confirm"
  cancelBtnText="Cancel"
  onDateChange={(date) => {this.setState({date: date})}}
/>

react-native-modal-datetime-picker:

<DateTimePickerModal
  isVisible={isDatePickerVisible}
  mode="date"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

The react-native-datepicker component offers more inline customization options, while react-native-modal-datetime-picker focuses on a modal-based approach with simpler props. The latter provides a more native feel on both iOS and Android platforms.

1,486

Picker is a cross-platform UI component for selecting an item from a list of options.

Pros of picker

  • More lightweight and focused on general-purpose picking
  • Offers a wider range of customization options for the picker UI
  • Supports both iOS and Android with a single API

Cons of picker

  • Lacks built-in modal functionality, requiring additional setup for modal display
  • Does not provide specific date and time picking functionality out of the box
  • May require more code to implement a datetime picker specifically

Code Comparison

react-native-modal-datetime-picker:

<DateTimePickerModal
  isVisible={isDatePickerVisible}
  mode="datetime"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

picker:

<Picker
  selectedValue={selectedValue}
  style={{ height: 50, width: 150 }}
  onValueChange={(itemValue, itemIndex) => setSelectedValue(itemValue)}
>
  <Picker.Item label="Option 1" value="option1" />
  <Picker.Item label="Option 2" value="option2" />
</Picker>

The react-native-modal-datetime-picker provides a more streamlined approach for datetime picking with built-in modal functionality. In contrast, picker offers a more versatile solution for various types of pickers but requires additional setup for datetime picking and modal display.

🔽 A Picker component for React Native which emulates the native <select> interfaces for iOS and Android

Pros of react-native-picker-select

  • More versatile, allowing for custom picker options beyond just date and time
  • Simpler implementation for basic selection needs
  • Supports both iOS and Android with a consistent API

Cons of react-native-picker-select

  • Less specialized for date and time picking, which may require additional configuration
  • Doesn't provide built-in modal functionality, requiring separate implementation if needed

Code Comparison

react-native-picker-select:

import RNPickerSelect from 'react-native-picker-select';

<RNPickerSelect
  onValueChange={(value) => console.log(value)}
  items={[
    { label: 'Football', value: 'football' },
    { label: 'Baseball', value: 'baseball' },
    { label: 'Hockey', value: 'hockey' },
  ]}
/>

react-native-modal-datetime-picker:

import DateTimePickerModal from "react-native-modal-datetime-picker";

<DateTimePickerModal
  isVisible={isDatePickerVisible}
  mode="date"
  onConfirm={handleConfirm}
  onCancel={hideDatePicker}
/>

The code comparison shows that react-native-picker-select is more flexible for general selection purposes, while react-native-modal-datetime-picker is specifically designed for date and time picking with built-in modal functionality. The choice between these libraries depends on the specific requirements of your project, with react-native-picker-select being more suitable for general selection needs and react-native-modal-datetime-picker being optimal for date and time picking scenarios.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

react-native-modal-datetime-picker

npm version Supports Android and iOS

A declarative cross-platform react-native date and time picker.

This library exposes a cross-platform interface for showing the native date-picker and time-picker inside a modal, providing a unified user and developer experience.

Under the hood, this library is using @react-native-community/datetimepicker.

Setup (for non-Expo projects)

If your project is not using Expo, install the library and the community date/time picker using npm or yarn:

# using npm
$ npm i react-native-modal-datetime-picker @react-native-community/datetimepicker

# using yarn
$ yarn add react-native-modal-datetime-picker @react-native-community/datetimepicker

Please notice that the @react-native-community/datetimepicker package is a native module so it might require manual linking.

Setup (for Expo projects)

If your project is using Expo, install the library and the community date/time picker using the Expo CLI:

npx expo install react-native-modal-datetime-picker @react-native-community/datetimepicker

To ensure the picker theme respects the device theme, you should also configure the appearance styles in your app.json this way:

{
  "expo": {
    "userInterfaceStyle": "automatic"
  }
}

Refer to the Appearance documentation on Expo for more info.

Usage

import React, { useState } from "react";
import { Button, View } from "react-native";
import DateTimePickerModal from "react-native-modal-datetime-picker";

const Example = () => {
  const [isDatePickerVisible, setDatePickerVisibility] = useState(false);

  const showDatePicker = () => {
    setDatePickerVisibility(true);
  };

  const hideDatePicker = () => {
    setDatePickerVisibility(false);
  };

  const handleConfirm = (date) => {
    console.warn("A date has been picked: ", date);
    hideDatePicker();
  };

  return (
    <View>
      <Button title="Show Date Picker" onPress={showDatePicker} />
      <DateTimePickerModal
        isVisible={isDatePickerVisible}
        mode="date"
        onConfirm={handleConfirm}
        onCancel={hideDatePicker}
      />
    </View>
  );
};

export default Example;

Available props

👉 Please notice that all the @react-native-community/react-native-datetimepicker props are supported as well!

NameTypeDefaultDescription
buttonTextColorIOSstringThe color of the confirm button texts (iOS)
backdropStyleIOSstyleThe style of the picker backdrop view style (iOS)
cancelButtonTestIDstringUsed to locate cancel button in end-to-end tests
cancelTextIOSstring"Cancel"The label of the cancel button (iOS)
confirmButtonTestIDstringUsed to locate confirm button in end-to-end tests
confirmTextIOSstring"Confirm"The label of the confirm button (iOS)
customCancelButtonIOScomponentOverrides the default cancel button component (iOS)
customConfirmButtonIOScomponentOverrides the default confirm button component (iOS)
customHeaderIOScomponentOverrides the default header component (iOS)
customPickerIOScomponentOverrides the default native picker component (iOS)
dateobjnew Date()Initial selected date/time
isVisibleboolfalseShow the datetime picker?
isDarkModeEnabledbool?undefinedForces the picker dark/light mode if set (otherwise fallbacks to the Appearance color scheme) (iOS)
modalPropsIOSobject{}Additional modal props for iOS
modalStyleIOSstyleStyle of the modal content (iOS)
modestring"date"Choose between "date", "time", and "datetime"
onCancelfuncREQUIREDFunction called on dismiss
onChangefunc() => nullFunction called when the date changes (with the new date as parameter).
onConfirmfuncREQUIREDFunction called on date or time picked. It returns the date or time as a JavaScript Date object
onHidefunc() => nullCalled after the hide animation
pickerContainerStyleIOSstyleThe style of the picker container (iOS)
pickerStyleIOSstyleThe style of the picker component wrapper (iOS)
pickerComponentStyleIOSstyleThe style applied to the actual picker component - this can be either a native iOS picker or a custom one if customPickerIOS was provided

Frequently Asked Questions

This repo is only maintained by me, and unfortunately I don't have enough time for dedicated support & question. If you're experiencing issues, please check the FAQs below.
For questions and support, please start try starting a discussion or try asking it on StackOverflow.
⚠️ Please use the GitHub issues only for well-described and reproducible bugs. Question/support issues will be closed.

The component is not working as expected, what should I do?

Under the hood react-native-modal-datetime-picker uses @react-native-community/datetimepicker. If you're experiencing issues, try swapping react-native-datetime-picker with @react-native-community/datetimepicker. If the issue persists, check if it has already been reported as a an issue or check the other FAQs.

How can I show the timepicker instead of the datepicker?

Set the mode prop to time. You can also display both the datepicker and the timepicker in one step by setting the mode prop to datetime.

Why is the initial date not working?

Please make sure you're using the date props (and not the value one).

Can I use the new iOS 14 style for the date/time picker?

Yes!
You can set the display prop (that we'll pass down to react-native-datetimepicker) to inline to use the new iOS 14 picker.

Please notice that you should probably avoid using this new style with a time-only picker (so with mode set to time) because it doesn't suit well this use case.

Why does the picker show up twice on Android?

This seems to be a known issue of the @react-native-community/datetimepicker. Please see this thread for a couple of workarounds. The solution, as described in this reply is hiding the modal, before doing anything else.

Example of solution using Input + DatePicker

The most common approach for solving this issue when using an Input is:

  • Wrap your Input with a "Pressable"/Button (TouchableWithoutFeedback/TouchableOpacity + activeOpacity={1} for example)
  • Prevent Input from being focused. You could set editable={false} too for preventing Keyboard opening
  • Triggering your hideModal() callback as a first thing inside onConfirm/onCancel callback props
const [isVisible, setVisible] = useState(false);
const [date, setDate] = useState('');

<TouchableOpacity
  activeOpacity={1}
  onPress={() => setVisible(true)}>
  <Input
    value={value}
    editable={false} // optional
  />
</TouchableOpacity>
<DatePicker
  isVisible={isVisible}
  onConfirm={(date) => {
    setVisible(false); // <- first thing
    setValue(parseDate(date));
  }}
  onCancel={() => setVisible(false)}
/>

How can I allow picking only specific dates?

You can't — @react-native-community/datetimepicker doesn't allow you to do so. That said, you can allow only "range" of dates by setting a minimum and maximum date. See below for more info.

How can I set a minimum and/or maximum date?

You can use the minimumDate and maximumDate props from @react-native-community/datetimepicker.

How do I change the color of the Android date and time pickers?

This is more a React-Native specific question than a react-native-modal-datetime-picker one.
See issue #29 and #106 for some solutions.

How to set a 24-hours format in iOS?

The is24Hour prop is only available on Android but you can use a small hack for enabling it on iOS by setting the picker timezone to en_GB:

<DatePicker
  mode="time"
  locale="en_GB" // Use "en_GB" here
  date={new Date()}
/>

How can I change the picker language/locale?

Under the hood this library is using @react-native-community/datetimepicker. You can't change the language/locale from react-native-modal-datetime-picker. Locale/language is set at the native level, on the device itself.

How can I set an automatic locale in iOS?

On iOS, you can set an automatic detection of the locale (fr_FR, en_GB, ...) depending on the user's device locale. To do so, edit your AppDelegate.m file and add the following to didFinishLaunchingWithOptions.

// Force DatePicker locale to current language (for: 24h or 12h format, full day names etc...)
NSString *currentLanguage = [[NSLocale preferredLanguages] firstObject];
[[UIDatePicker appearance] setLocale:[[NSLocale alloc]initWithLocaleIdentifier:currentLanguage]];

Why is the picker is not showing the right layout on iOS >= 14?

Please make sure you're on the latest version of react-native-modal-datetime-picker and of the @react-native-community/datetimepicker. We already closed several iOS 14 issues that were all caused by outdated/cached versions of the community datetimepicker.

Why is the picker not visible/transparent on iOS?

Please make sure you're on the latest version of react-native-modal-datetime-picker and of @react-native-community/datetimepicker. Also, double-check that the picker light/dark theme is aligned with the OS one (e.g., don't "force" a theme using isDarkModeEnabled).

Why can't I show an alert after the picker has been hidden (on iOS)?

Unfortunately this is a know issue with React-Native on iOS. Even by using the onHide callback exposed by react-native-modal-datetime-picker you might not be able to show the (native) alert successfully. The only workaround that seems to work consistently for now is to wrap showing the alter in a setTimeout 😔:

const handleHide = () => {
  setTimeout(() => Alert.alert("Hello"), 0);
};

See issue #512 for more info.

Why does the date of onConfirm not match the picked date (on iOS)?

On iOS, clicking the "Confirm" button while the spinner is still in motion — even just slightly in motion — will cause the onConfirm callback to return the initial date instead of the picked one. This is is a long standing iOS issue (that can happen even on native app like the iOS calendar) and there's no failproof way to fix it on the JavaScript side.
See this GitHub gist for an example of how it might be solved at the native level — but keep in mind it won't work on this component until it has been merged into the official React-Native repo.

Related issue in the React-Native repo here.

How do I make it work with snapshot testing?

See issue #216 for a possible workaround.

Contributing

Please see the contributing guide.

License

The library is released under the MIT license. For more details see LICENSE.

NPM DownloadsLast 30 Days