What is @react-native-community/datetimepicker?
@react-native-community/datetimepicker is a React Native module that provides a cross-platform Date and Time Picker component. It allows developers to easily integrate date and time selection functionality into their React Native applications, supporting both iOS and Android platforms.
What are @react-native-community/datetimepicker's main functionalities?
Date Picker
This feature allows users to select a date from a calendar interface. The code sample demonstrates how to display a date picker when a button is pressed and update the state with the selected date.
import DateTimePicker from '@react-native-community/datetimepicker';
const App = () => {
const [date, setDate] = useState(new Date());
const [show, setShow] = useState(false);
const onChange = (event, selectedDate) => {
const currentDate = selectedDate || date;
setShow(false);
setDate(currentDate);
};
return (
<View>
<Button onPress={() => setShow(true)} title="Show Date Picker" />
{show && (
<DateTimePicker
value={date}
mode="date"
display="default"
onChange={onChange}
/>
)}
</View>
);
};
Time Picker
This feature allows users to select a time from a clock interface. The code sample demonstrates how to display a time picker when a button is pressed and update the state with the selected time.
import DateTimePicker from '@react-native-community/datetimepicker';
const App = () => {
const [time, setTime] = useState(new Date());
const [show, setShow] = useState(false);
const onChange = (event, selectedTime) => {
const currentTime = selectedTime || time;
setShow(false);
setTime(currentTime);
};
return (
<View>
<Button onPress={() => setShow(true)} title="Show Time Picker" />
{show && (
<DateTimePicker
value={time}
mode="time"
display="default"
onChange={onChange}
/>
)}
</View>
);
};
Date and Time Picker
This feature allows users to select both date and time from a combined interface. The code sample demonstrates how to display a date and time picker when a button is pressed and update the state with the selected date and time.
import DateTimePicker from '@react-native-community/datetimepicker';
const App = () => {
const [dateTime, setDateTime] = useState(new Date());
const [show, setShow] = useState(false);
const onChange = (event, selectedDateTime) => {
const currentDateTime = selectedDateTime || dateTime;
setShow(false);
setDateTime(currentDateTime);
};
return (
<View>
<Button onPress={() => setShow(true)} title="Show DateTime Picker" />
{show && (
<DateTimePicker
value={dateTime}
mode="datetime"
display="default"
onChange={onChange}
/>
)}
</View>
);
};
Other packages similar to @react-native-community/datetimepicker
react-native-datepicker
react-native-datepicker is another popular package for date and time picking in React Native applications. It provides a customizable date and time picker component. Compared to @react-native-community/datetimepicker, it offers more styling options but may not be as tightly integrated with the native date and time pickers on iOS and Android.
react-native-modal-datetime-picker
react-native-modal-datetime-picker is a React Native module that provides a customizable modal date and time picker. It wraps the native date and time pickers in a modal, offering more flexibility in terms of UI customization. It is a good alternative if you need a modal-based picker with more styling options compared to @react-native-community/datetimepicker.
React Native DateTimePicker
React Native date & time picker component for iOS and Android
Table of Contents
Getting started
npm install @react-native-community/datetimepicker --save
or
yarn add @react-native-community/datetimepicker
Install using react-native link
react-native link @react-native-community/datetimepicker
Manual installation
iOS
Android
-
Add the following lines to android/settings.gradle
:
include ':react-native-datetimepicker'
project(':react-native-datetimepicker').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/datetimepicker/android')
-
Add into android/gradle.properties
:
android.useAndroidX=true
android.enableJetifier=true
-
Add the compile line to the dependencies in android/app/build.gradle
:
dependencies {
...
implementation project(':react-native-datetimepicker')
}
-
Add the import and link the package in MainApplication.java
:
+ import com.reactcommunity.rndatetimepicker.RNDateTimePickerPackage;
public class MainApplication extends Application implements ReactApplication {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
+ new RNDateTimePickerPackage()
);
}
}
General Usage
Importing it
import DateTimePicker from '@react-native-community/datetimepicker';
or
var DateTimePicker = require('@react-native-community/datetimepicker');
Basic usage with state
import React, {Component} from 'react';
import {View, Button, Platform} from 'react-native';
import DateTimePicker from '@react-native-community/datetimepicker';
export default class App extends Component {
state = {
date: new Date('2020-06-12T14:42:42'),
mode: 'date',
show: false,
}
setDate = (event, date) => {
date = date || this.state.date;
this.setState({
show: Platform.OS === 'ios' ? true : false,
date,
});
}
show = mode => {
this.setState({
show: true,
mode,
});
}
datepicker = () => {
this.show('date');
}
timepicker = () => {
this.show('time');
}
render() {
const { show, date, mode } = this.state;
return (
<View>
<View>
<Button onPress={this.datepicker} title="Show date picker!" />
</View>
<View>
<Button onPress={this.timepicker} title="Show time picker!" />
</View>
{ show && <DateTimePicker value={date}
mode={mode}
is24Hour={true}
display="default"
onChange={this.setDate} />
}
</View>
);
}
}
Props
mode
(optional
)
Defines the type of the picker.
List of possible values:
"date"
(default for iOS
and `Android)"time"
"datetime"
(iOS
only)"countdown"
(iOS
only)
<RNDateTimePicker mode="time" />
display
(optional
, Android only
)
Defines the visual display of the picker for Android and will be ignored for iOS.
List of possible values:
"default"
"spinner"
"calendar"
(only for date
mode)"clock"
(only for time
mode)
<RNDateTimePicker display="spinner" } />
onChange
(optional
)
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.
setDate = (event, date) => {}
<RNDateTimePicker onChange={this.setDate} />
value
(required
)
Defines the date or time value used in the component.
<RNDateTimePicker value={new Date()} />
maximumDate
(optional
)
Defines the maximum date that can be selected.
<RNDateTimePicker maximumDate={new Date(2300, 10, 20)} />
minimumDate
(optional
)
Defines the minimum date that can be selected.
<RNDateTimePicker minimumDate={new Date(1950, 0, 1)} />
timeZoneOffsetInMinutes
(optional
, iOS only
)
Allows changing of the timeZone of the date picker. By default it uses the device's time zone.
<RNDateTimePicker timeZoneOffsetInMinutes={60} />
locale
(optional
, iOS only
)
Allows changing of the locale of the component. By default it uses the device's locale.
<RNDateTimePicker locale="es-ES" />
is24Hour
(optional
, Android only
)
Allows changing of the time picker to a 24 hour format.
<RNDateTimePicker is24Hour={true} />
minuteInterval
(optional
, iOS only
)
The interval at which minutes can be selected.
Possible values are: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30
<RNDateTimePicker minuteInterval={10} />
Migration from the older components
RNDateTimePicker
is the new common name used to represent the old versions of iOS and Android.
On Android, open picker modals will update the selected date and/or time if the prop value
changes. For example, if a HOC holding state, updates the value
prop. Previously the component used to close the modal and render a new one on consecutive calls.
DatePickerIOS
-
initialDate
is deprecated, use value
instead.
<DatePickerIOS initialValue={new Date()} />
<RNDateTimePicker value={new Date()} />
-
date
is deprecated, use value
instead.
<DatePickerIOS date={new Date()} />
<RNDateTimePicker value={new Date()} />
-
onChange
now returns also the date.
onChange = (event) => {}
<DatePickerIOS onChange={this.onChange} />
onChange = (event, date) => {}
<RNDateTimePicker onChange={this.onChange} />
-
onDateChange
is deprecated, use onChange
instead.
setDate = (date) => {}
<DatePickerIOS onDateChange={this.setDate} />
setDate = (event, date) => {}
<RNDateTimePicker onChange={this.setDate} />
DatePickerAndroid
-
date
is deprecated, use value
instead.
try {
const {action, year, month, day} = await DatePickerAndroid.open({
date: new Date()
});
} catch ({code, message}) {
console.warn('Cannot open date picker', message);
}
<RNDateTimePicker mode="date" value={new Date()} />
-
minDate
and maxDate
are deprecated, use minimumDate
and maximumDate
instead.
try {
const {action, year, month, day} = await DatePickerAndroid.open({
minDate: new Date(),
maxDate: new Date()
});
} catch ({code, message}) {
console.warn('Cannot open date picker', message);
}
<RNDateTimePicker mode="date" minimumDate={new Date()} maximumDate={new Date()} />
-
dateSetAction
is deprecated, use onChange
instead.
try {
const {action, year, month, day} = await DatePickerAndroid.open();
if (action === DatePickerAndroid.dateSetAction) {
}
} catch ({code, message}) {
console.warn('Cannot open date picker', message);
}
setDate = (event, date) => {
if (date !== undefined) {
}
}
<RNDateTimePicker mode="date" onChange={this.setDate} />
-
dismissedAction
is deprecated, use onChange
instead.
try {
const {action, year, month, day} = await DatePickerAndroid.open();
if (action === DatePickerAndroid.dismissedAction) {
}
} catch ({code, message}) {
console.warn('Cannot open date picker', message);
}
setDate = (event, date) => {
if (date === undefined) {
}
}
<RNDateTimePicker mode="date" onChange={this.setDate} />
TimePickerAndroid
-
hour
and minute
are deprecated, use value
instead.
try {
const {action, hour, minute} = await TimePickerAndroid.open({
hour: 14,
minute: 0,
is24Hour: false,
});
if (action !== TimePickerAndroid.dismissedAction) {
}
} catch ({code, message}) {
console.warn('Cannot open time picker', message);
}
<RNDateTimePicker mode="time" value={new Date()} />
-
timeSetAction
is deprecated, use onChange
instead.
try {
const {action, hour, minute} = await TimePickerAndroid.open();
if (action === TimePickerAndroid.timeSetAction) {
}
} catch ({code, message}) {
console.warn('Cannot open time picker', message);
}
setTime = (event, date) => {
if (date !== undefined) {
}
}
<RNDateTimePicker mode="time" onChange={this.setTime} />
-
dismissedAction
is deprecated, use onChange
instead.
try {
const {action, hour, minute} = await TimePickerAndroid.open();
if (action === TimePickerAndroid.dismissedAction) {
}
} catch ({code, message}) {
console.warn('Cannot open time picker', message);
}
setTime = (event, date) => {
if (date === undefined) {
}
}
<RNDateTimePicker mode="time" onChange={this.setTime} />
Contributing to the component
Clone, install
git clone https://github.com/react-native-community/react-native-datetimepicker.git
cd react-native-datetimepicker
npm install
Tests
Jest
npm install
npm run test
Detox
Detox is a gray box end-to-end testing and automation library for mobile apps.
For cleaning all the detox builds just run npm run detox:clean
.
iOS
Android
An existing Android emulator is required to match the name defined in detox.configurations.android.emu.debug.name
and detox.configurations.android.emu.release.name
inside the package.json
.
Running the example app
- Run
npm run start
for starting Metro Bundler - Run
npm run start:ios
or npm run start:android