Hello Friends,

Welcome To Infinitbility!

DatePickerAndroid provide by react native you don’t need to install packages for it.

for import DatePickerAndroid from react native

import { DatePickerAndroid } from 'react-native';

DatePickerAndroid

Deprecated. Use @react-native-community/datetimepicker instead.

To show date picker in react native call below function on your code


// call function for show date picker
async openDatePicker(){

    try {
        const {
            action,
            year,
            month,
            day
        } = await DatePickerAndroid.open({
            // Use `new Date()` for current date.
            // May 25 2020. Month 0 is January.
            date: new Date(2020, 4, 25) // show selected date
        });
        if (action !== DatePickerAndroid.dismissedAction) {
            // Selected year, month (0-11), day

            this.setState({date: new Date(year, month, day)});
        }
    } catch ({ code, message }) {
        console.warn('Cannot open date picker', message);
    }
}

props

show selected current date on datepicker you want

await DatePickerAndroid.open({
    date : new Date() 
});

For show disabled previous date on datepicker

await DatePickerAndroid.open({
    minDate : new Date() 
});

for show disabled upcoming date on datepicker

await DatePickerAndroid.open({
    maxDate  : new Date()
});

change datepicker layout like calendar, spinner, and default.

await DatePickerAndroid.open({
    mode: 'spinner' // enum('calendar', 'spinner', 'default')
});

write code after press cancel on datepicker

if (action == DatePickerAndroid.dismissedAction) {
    // code goes here
}

write code after press ok on datepicker

if (action == DatePickerAndroid.dateSetAction) {
    // code goes here
}

chage datepicker theme style

DatePickerAndroid is an API for the native DatePicker Android module and thus cannot be directly styled through JS props like real React Native components. However, you can style the native android modules by changing the styles.xml in your android folder. It is located at “android/app/src/main/res/values/styles.xml” relative to your react-native project. To get a red background for the DatePicker, you edit the contents of styles.xml as follows:

Make Sure your datePickerAndroid mode is default else style not working.


<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="android:datePickerDialogTheme">@style/Dialog.Theme</item>
    </style>

	<style name="Dialog.Theme" parent="Theme.AppCompat.Light.Dialog">
	    <item name="colorAccent">#FF0000</item>
	    <item name="android:textColorPrimary">#0000FF</item>
 	</style>
</resources>

Multipe Fields for datepicker

when you have Multipe input Fields of date then pass flag on openDatePicker function. like below example.


    async openDatePicker(dateType){
        try {
            const {
                action,
                year,
                month,
                day
            } = await DatePickerAndroid.open({
                // Use `new Date()` for current date.
                // May 25 2020. Month 0 is January.
                minDate: new Date(this.state.selectedStartDate),
                date: new Date(),
                mode: 'default'
            });
            if (action !== DatePickerAndroid.dismissedAction) {
                let date = new Date(year, month, day);

                if(dateType == 'start'){
                    this.setState({selectedStartDate: date});
                }else{
                    this.setState({selectedEndDate: date});
                }

            }
        } catch ({ code, message }) {
            console.warn('Cannot open date picker', message);
        }
    }

Thanks for reading…

More From React Native Tutorial

Basics

1. Introduction To React Native

2. React Native Environment Setup using expo

3. React Native Environment Setup for windows

4. React Native Environment setup on Mac OS

5. React Native Environment setup on linux

6. React Native Project Structure

7. React Native State

8. React Native Props

9. React Native Styling

10. React Native Flexbox

11. React Native Text

12. React Native Textinput

13. React Native Commands

14. React Native ScrollView

Advances

1. React Native Dark Mode

2. React Native Fonts

3. React Native SQLite

4. React Native DatepickerAndroid

5. React native ScrollView scroll to position

6. How to align icon with text in react native

7. React Native Image

8. React Native Firebase Crashlytics

9. React Native Async Storage

10. React Native Share

Error & Issue Solution

1. Task :app:transformDexArchiveWithDexMergerForDebug FAILED In React Native

2. Expiring Daemon because JVM heap space is exhausted In React Native

3. Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED In React Native

4. Unable to determine the current character, it is not a string, number, array, or object in react native

5. App crashed immediately after install react native video or track player

6. how to delete SQLite database in android react native

7. React native material dropdown twice click issue

8. How to get the current route in react-navigation?

9. how to disable drawer on the drawer navigation screen?

10. Image not showing in ios 14 react native

11. React Native image picker launchimagelibrary on second time issue

12. how to open any link from react native render Html

13. Network request failed in react native fetch

14. React Native upload video example