Hello Friends đź‘‹,

Welcome to Infinitbility.

Many devs searching for set current date in datepicker i.e we have decided to make tutprial on this topic.

Today, we are using react-native-datepicker lib to set current date example but you are open to use any lib it will work.

React native datepicker lib provide date props to get value from state and Javascript provide current date when we write new Date(). So we only store new Date() in state.

import React, { Component } from 'react'
import { View } from 'react-native'
import DatePicker from 'react-native-datepicker'

export default class MyDatePicker extends Component {
  constructor(props){
    super(props)
    this.state = {date: new Date()}
  }

  render(){
    return (
      <View style={{flex: 1, justifyContent: "center", alignItems: "center"}}>
      <DatePicker
        style={{width: 200}}
        date={this.state.date}
        mode="date"
        placeholder="select date"
        format="YYYY-MM-DD"
        minDate="2016-05-01"
        maxDate="2016-06-01"
        confirmBtnText="Confirm"
        cancelBtnText="Cancel"
        customStyles={{
          dateIcon: {
            position: 'absolute',
            left: 0,
            top: 4,
            marginLeft: 0
          },
          dateInput: {
            marginLeft: 36
          }
          // ... You can check the source to find the other keys.
        }}
        onDateChange={(date) => {this.setState({date: date})}}
      />
      </View>
    )
  }
}

Output

React native datepicker current date example
React native datepicker current date example

Conclusion

When we pass new Date() obeject in any datepicker it will show current date selected default.

Thanks for reading…