3

I am using react-datepicker but for some reason, we don't want to show the month and year in the dropdown.

<DatePicker
    selected={this.state.startDate}
    onChange={this.handleChange}
    peekNextMonth
    showMonthDropdown
    showYearDropdown
    dropdownMode="select"
/>

Is there any other way to show the month and year?

Gorakh Nath
  • 9,140
  • 15
  • 46
  • 68

4 Answers4

7

To show only Month and Year Dropdown, try this.

<DatePicker
  dateFormat="MMMM yyyy"
  showMonthYearPicker
  selected={this.state.startDate}
  onChange={this.handleChange}
/>

ref: link

MDK
  • 131
  • 2
  • 4
1

There is an inline version for react-datepicker

<DatePicker
    inline
    selected={this.state.startDate}
    onChange={this.handleChange}
/>

ref: https://hacker0x01.github.io/react-datepicker/ Scroll to bottom on this page to see the example of the inline version

Hana Alaydrus
  • 2,225
  • 16
  • 19
  • above code won't give the multiple options to the user for selecting month and year. – Gorakh Nath Sep 05 '17 at 10:06
  • @jack123 sorry I don't really understand what you looking for, any picture or drawing? so I can undestand more. – Hana Alaydrus Sep 05 '17 at 10:13
  • these two property showMonthDropdow and showYearDropdown use to show the month and year, but if you will use it you will see that option to select month and year are coming in the dropdown.But I don't to show the option in dropdown. – Gorakh Nath Sep 05 '17 at 10:18
  • 1
    I dont see it possible in react-datepicker, but there are other date picker library http://clauderic.github.io/react-infinite-calendar/#/customization/select-year-first?_k=eht357 but you might need some tweak to make it looks like what you want – Hana Alaydrus Sep 05 '17 at 11:02
1

There are two modes "select" and "scroll".

<DatePicker
    selected={this.state.startDate}
    onChange={this.handleChange}
    peekNextMonth
    showMonthDropdown
    showYearDropdown
    dropdownMode= "scroll"
/>
Raubin
  • 11
  • 2
0

showYearDropdown works only if you set minDate prop with date object. Ex minDate={new Date()}. If you pass string to minDate then year dropdown will be empty.

Avoid using minDate={parseISO(new Date())}, where parseISO returns date string.