Hasangi
Hasangi

Reputation: 320

DatePicker NativeBase - Format of the picked date

I am using DatePicker of NativeBase and want to change the format of the displayed date after picking a date. I am unable to find a relevant prop due to lack of docs.

Is there a way I could change the format of the date as in DatePickerAndroid using format="YYYY-MM-DD"?

Upvotes: 2

Views: 3811

Answers (2)

Newana
Newana

Reputation: 71

Fixed in native-base v2.6.1 onwards.

<DatePicker
formatChosenDate={date => {return moment(date).format('YYYY-MM-DD');}}
..... />

Upvotes: 5

Pramod
Pramod

Reputation: 1940

Try this:----

async onPressAction() {
    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(2018, 6, 26)
    });
    if (action !== DatePickerAndroid.dismissedAction) {
      // Selected year, month (0-11), day
      var date = new Date(year, month, day);
      var year = date.getFullYear();
      var month = date.getMonth()+1;
      var day = date.getDate();
      console.log('>>>>>>>>>>>>>'+year);
      console.log('>>>>>>>>>>>>>'+month);
      console.log('>>>>>>>>>>>>>'+day);
      console.log(year+'-'+month+'-'+day);

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

Upvotes: 0

Related Questions