Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork448
React Native date & time picker component for iOS, Android and Windows
License
react-native-datetimepicker/datetimepicker
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Please support maintenance of the module with a monthly donation or help us with issues and pull requests.
Become a backer on OpenCollective orsponsor us on GitHub Sponsors.
See thisissue for context. Thank you!
This repository was moved out of the react native community GH organization, in accordance tothis proposal.The module is still published onnpm under the old namespace (as documented) but will be published under a new namespace at some point, with a major version bump.
React Native date & time picker component for iOS, Android and Windows (please note Windows is not actively maintained).
- React Native DateTimePicker
- Table of contents
- Expo users notice
- Getting started
- Usage
- React Native Support
- Localization note
- Android imperative API
- Android styling
- Props / params
mode(optional)display(optional)design(optional,Android only)initialInputMode(optional,Android only)title(optional,Android only)fullscreen(optional,Android only)onChange(optional)value(required)maximumDate(optional)minimumDate(optional)timeZoneName(optional,iOS or Android only)timeZoneOffsetInMinutes(optional,iOS or Android only)timeZoneOffsetInSeconds(optional,Windows only)dayOfWeekFormat(optional,Windows only)dateFormat(optional,Windows only)firstDayOfWeek(optional,Windows only)textColor(optional,iOS only)accentColor(optional,iOS only)themeVariant(optional,iOS only)locale(optional,iOS only)is24Hour(optional,Windows and Android only)positiveButton(optional,Android only)negativeButton(optional,Android only)neutralButton(optional,Android only)minuteInterval(optional)style(optional,iOS only)disabled(optional,iOS only)view props(optional,iOS only)onError(optional,Android only)
- Testing with Jest
- Migration from the older components
- Contributing to the component
- Manual installation
- Running the example app
- Only Android API level >=21 (Android 5), iOS >= 11 are supported.
- Tested with Xcode 14.0 and RN 0.72.7. Other configurations are very likely to work as well but have not been tested.
The module supports thenew React Native architecture (Fabric rendering of iOS components, and turbomodules on Android). If you are using the new architecture, you will need to use React Native 0.71.4 or higher.
This module is part of Expo Managed Workflow -see docs. However, Expo SDK in the Managed Workflow may not contain the latest version of the module and therefore, the newest features and bugfixes may not be available in Expo Managed Workflow.If you use the Managed Workflow, use the commandexpo install @react-native-community/datetimepicker (notyarn ornpm) to install this module - Expo will automatically install the latest version compatible with your Expo SDK (which maynot be the latest version of the module available).
If you're using aDev Client, rebuild the Dev Client after installing the dependencies.
If you're using theexpo prebuild command and building your native app projects (e.g. with EAS Build or locally), you can use the latest version of the module.
npm install @react-native-community/datetimepicker --save
or
yarn add @react-native-community/datetimepicker
Autolinking is not yet implemented on Windows, somanual installation is needed.
If you are using RN >= 0.60, only runnpx pod-install. Then rebuild your project.
Check thereact-native version support table below to find the correspondingdatetimepicker version to meet support requirements. Maintenance is only provided for last 3 stable react-native versions.
| react-native version | version |
|---|---|
| 0.73.0+ | 7.6.3+ |
| <=0.72.0 | <=7.6.2 |
| 0.70.0+ | 7.0.1+ |
| <0.70.0 | <=7.0.0 |
importDateTimePickerfrom'@react-native-community/datetimepicker';
Expand for examples
We give two equivalent examples on how to use the package on all supported platforms.
While the component-approach as given in the second paragraph works on Android, the recommended approach is to use the imperative api given in the first paragraph.
Read more about the motivation inAndroid imperative API.
exportconstApp=()=>{const[date,setDate]=useState(newDate(1598051730000));constonChange=(event,selectedDate)=>{constcurrentDate=selectedDate;setDate(currentDate);};constshowMode=(currentMode)=>{DateTimePickerAndroid.open({value:date, onChange,mode:currentMode,is24Hour:true,});};constshowDatepicker=()=>{showMode('date');};constshowTimepicker=()=>{showMode('time');};return(<SafeAreaView><ButtononPress={showDatepicker}title="Show date picker!"/><ButtononPress={showTimepicker}title="Show time picker!"/><Text>selected:{date.toLocaleString()}</Text></SafeAreaView>);};
exportconstApp=()=>{const[date,setDate]=useState(newDate(1598051730000));const[mode,setMode]=useState('date');const[show,setShow]=useState(false);constonChange=(event,selectedDate)=>{constcurrentDate=selectedDate;setShow(false);setDate(currentDate);};constshowMode=(currentMode)=>{setShow(true);setMode(currentMode);};constshowDatepicker=()=>{showMode('date');};constshowTimepicker=()=>{showMode('time');};return(<SafeAreaView><ButtononPress={showDatepicker}title="Show date picker!"/><ButtononPress={showTimepicker}title="Show time picker!"/><Text>selected:{date.toLocaleString()}</Text>{show&&(<DateTimePickertestID="dateTimePicker"value={date}mode={mode}is24Hour={true}onChange={onChange}/>)}</SafeAreaView>);};
By localization, we refer to the language (names of months and days), as well as order in which date can be presented in a picker (month/day vs. day/month) and 12 / 24 hour-format.
On Android, the picker will be controlled by the system locale. If you wish to change it,see instructions here.
On iOS, use XCode, asdocumented here to inform the OS about the locales your application supports. iOS will automatically display the correctly localized DateTimePicker as long as the target language is contained inproject.pbxproj.
If you use a library likei18next orreact-localize-redux to manage your translations, it is sufficient to add your target languages as described in the Apple Documentation - but you are not required to add any localization keys (like, for example, the days of the week). iOS will automatically display the correct localized strings as long as the target language is contained in
project.pbxproj.
For testing your localization setup, referhere.
There is also the iOS-only locale prop that can be used to force locale in some cases but its usage is discouraged due tonot working robustly in all picker modes (note the mixed month and day names). To the best of our knowledge, it works reliably in thespinner mode.
For Expo, follow thelocalization docs.
On Android, you have a choice between using the component API (regular React component) or an imperative api (think of something likeReactNative.alert()).
While the component API has the benefit of writing the same code on all platforms, for start we recommend using the imperative API on Android.
Theparams is an object with the same properties as the component props documented in the next paragraph. (This is also because the component api internally uses the imperative one.)
import{DateTimePickerAndroid}from'@react-native-community/datetimepicker';DateTimePickerAndroid.open(params:AndroidNativeProps)DateTimePickerAndroid.dismiss(mode:AndroidNativeProps['mode'])
The reason we recommend the imperative API is: on Android, the date/time picker opens in a dialog, similar toReactNative.alert() from core react native. The imperative api models this behavior better than the declarative component api. While the component approach is perfectly functional, based on the issue tracker history, it appears to be more prone to introducing bugs.
If you'd like to use the Material pickers, your app theme will need to inherit fromTheme.Material3.DayNight.NoActionBar instyles.xml.
Styling of the dialogs on Android can be easily customized by using the provided config plugin, provided that you use aExpo development build. The plugin allows you to configure color properties that cannot be set at runtime and requires building a new app binary to take effect.
Refer to this documentation for more information:android-styling.md.
Please note that this library currently exposes functionality from
UIDatePickeron iOS andDatePickerDialog +TimePickerDialog on Android, andCalendarDatePicker+TimePicker on Windows.These native classes offer only limited configuration, while there are dozens of possible options you as a developer may need. It follows that if your requirement is not supported by the backing native views, this library willnot be able to implement your requirement. When you open an issue with a feature request, please document if (or how) the feature can be implemented using the aforementioned native views. If the native views do not support what you need, such feature requests will be closed as not actionable.
Defines the type of the picker.
List of possible values:
"date"(default foriOSandAndroidandWindows)"time""datetime"(iOSonly)"countdown"(iOSonly)
<RNDateTimePickermode="time"/>
Defines the visual display of the picker. The default value is"default".
List of possible values for Android
"default"- Recommended. Show a default date picker (spinner/calendar/clock) based onmode."spinner""calendar"(only fordatemode)"clock"(only fortimemode)
List of possible values for iOS (maps topreferredDatePickerStyle)
"default"- Automatically pick the best style available for the current platform & mode."spinner"- the usual pre-iOS 14 appearance with a wheel from which you choose values"compact"- Affects only iOS 14 and later. Will fall back to "spinner" if not supported."inline"- Affects only iOS 14 and later. Will fall back to "spinner" if not supported.
<RNDateTimePickerdisplay="spinner"/>
Defines if the picker should use Material 3 components or the default picker. The default value is"default".
List of possible values
"default""material"
<RNDateTimePickerdesign="material"/>
Date change handler.
This is called when the user changes the date or time in the UI. It receives the event and the date as parameters.It is also called when user dismisses the picker, which you can detect by checking theevent.type property.The values can be:'set' | 'dismissed' | 'neutralButtonPressed'. (neutralButtonPressed is only available on Android).
TheutcOffset field is only available on Android and iOS. It is the offset in minutes between the selected date and UTC time.
constsetDate=(event:DateTimePickerEvent,date:Date)=>{const{ type,nativeEvent:{timestamp, utcOffset},}=event;};<RNDateTimePickeronChange={this.setDate}/>;
Defines the date or time value used in the component.
<RNDateTimePickervalue={newDate()}/>
Defines the maximum date that can be selected. Note that on Android, this only works fordate mode becauseTimePicker does not support this.
<RNDateTimePickermaximumDate={newDate(2030,10,20)}/>
Defines the minimum date that can be selected. Note that on Android, this only works fordate mode becauseTimePicker does not support this.
<RNDateTimePickerminimumDate={newDate(1950,0,1)}/>
Allows changing of the time zone of the date picker. By default, it uses the device's time zone.Use the time zone name from the IANA (TZDB) database name inhttps://en.wikipedia.org/wiki/List_of_tz_database_time_zones.
<RNDateTimePickertimeZoneName={'Europe/Prague'}/>
Allows changing of the time zone of the date picker. By default, it uses the device's time zone.Westrongly recommend usingtimeZoneName prop instead; this prop has known issues in the android implementation (eg.#528).
This prop will be removed in a future release.
// GMT+1<RNDateTimePickertimeZoneOffsetInMinutes={60}/>
Allows changing of the time zone of the date picker. By default, it uses the device's time zone.
// UTC+1<RNDateTimePickertimeZoneOffsetInSeconds={3600}/>
Sets the display format for the day of the week headers.Reference:https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.calendarview.dayofweekformat?view=winrt-18362#remarks
<RNDateTimePickerdayOfWeekFormat={'{dayofweek.abbreviated(2)}'}/>
Sets the display format for the date value in the picker's text box.Reference:https://docs.microsoft.com/en-us/uwp/api/windows.globalization.datetimeformatting.datetimeformatter?view=winrt-18362#examples
<RNDateTimePickerdateFormat="dayofweek day month"/>
Indicates which day is shown as the first day of the week.
<RNDateTimePickerfirstDayOfWeek={DAY_OF_WEEK.Wednesday}/>// The native parameter type is an enum defined in defined https://docs.microsoft.com/en-us/uwp/api/windows.globalization.dayofweek?view=winrt-18362 - meaning an integer needs to passed here (DAY_OF_WEEK).
Allows changing of the textColor of the date picker. Has effect only whendisplay is"spinner".
<RNDateTimePickertextColor="red"/>
Allows changing the accentColor (tintColor) of the date picker.Has no effect whendisplay is"spinner".
Allows overriding system theme variant (dark or light mode) used by the date picker.However, we recommend that you instead control the theme of the whole application usingreact-native-theme-control.
textColor to make the picker dark-theme compatible
List of possible values:
"light""dark"
<RNDateTimePickerthemeVariant="light"/>
Allows changing the locale of the component. This affects the displayed text and the date / time formatting. By default, the device's locale is used. Please note using this prop is discouraged due to not working reliably in all picker modes.Prefer localization as documented inLocalization note.
<RNDateTimePickerlocale="es-ES"/>
Allows changing of the time picker to a 24-hour format. By default, this value is decided automatically based on the locale and other preferences.
<RNDateTimePickeris24Hour={true}/>
design is "material". Allows setting the initial input mode of the picker.
List of possible values:
"default"- Recommended. Date pickers will show the calendar view by default, and time pickers will show the clock view by default."keyboard"- Both pickers will show an input where the user can type the date or time.
<RNDateTimePickerinitialInputMode="default"/>
design is "material". Allows setting the title of the dialog for the pickers.
<RNDateTimePickertitle="Choose anniversary"/>
design is "material". Allows setting the date picker dialog to be fullscreen.
<RNDateTimePickerfullscreen={true}/>
Set the positive button label and text color.
<RNDateTimePickerpositiveButton={{label:'OK',textColor:'green'}}/>
Allows displaying neutral button on picker dialog.Pressing button can be observed in onChange handler asevent.type === 'neutralButtonPressed'
<RNDateTimePickerneutralButton={{label:'Clear',textColor:'grey'}}/>
Set the negative button label and text color.
<RNDateTimePickernegativeButton={{label:'Cancel',textColor:'red'}}/>
Changes the label of the positive button.
<RNDateTimePickerpositiveButtonLabel="OK!"/>
Changes the label of the negative button.
<RNDateTimePickernegativeButtonLabel="Negative"/>
Allows displaying neutral button on picker dialog.Pressing button can be observed in onChange handler asevent.type === 'neutralButtonPressed'
<RNDateTimePickerneutralButtonLabel="clear"/>
The interval at which minutes can be selected.Possible values are:1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30
On Windows, this can be any number between 0-59.
on iOS, this in only supported whendisplay="spinner"
<RNDateTimePickerminuteInterval={10}/>
Sets style directly on picker component. By default, the picker dimensions are determined based on the props.
Please note that by default, picker's text color is controlled by the application theme (light / dark mode). In dark mode, text is white and in light mode, text is black.If you want to control the application theme, we recommend usingreact-native-theme-control.
This means that e.g. if the device has dark mode turned on, and your screen background color is white, you will not see the picker. Please use theAppearance api to adjust the picker's background color so that it is visible, as we do in theexample App.Alternatively, use thethemeVariant prop.
<RNDateTimePickerstyle={{flex:1}}/>
If true, the user won't be able to interact with the view.
Usually used by app automation frameworks.Fully supported on iOS. On Android, only supported formode="date".
<RNDateTimePickertestID="datePicker"/>
On iOS, you can pass anyView props to the component. Given that the underlying component is a native view, not all of them are guaranteed to be supported, buttestID andonLayout are known to work.
Callback that is called when an error occurs inside the date picker native code (such as null activity).
For examples of how you can write your tests, lookhere.
Please seemigration.md
Please seeCONTRIBUTING.md
Please seemanual-installation.md
- Run
yarnin repo root - Run
cd example - Install required pods by running
npx pod-install - Run
yarn startto start Metro Bundler - Run
yarn run start:iosoryarn run start:androidoryarn run start:windows - To do any development on the library, open the example project (in the
examplefolder) in xCode or Android Studio. The example project depends on the library code, which you can edit and observe any changes in the example project.
This project is tested with BrowserStack.
About
React Native date & time picker component for iOS, Android and Windows
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.








