'unable to display only year in react datepicker

Here is my below custom component for datepicker with yyyy format

 return (
      <DatePickerWrapper disable={disable} >
        <DatePicker
          name={this.props.name}
          selected={this.state.startDate}
          onChange={this.handleChange}
          onBlur={this.handleBlur}
          showYearPicker
          dateFormat={"yyyy"}
        />
      </DatePickerWrapper>
    );

I am still able to see it mm/dd/yyyy format



Solution 1:[1]

if i understand correctly You want it to be displayed for only a year, but what Displayed as "Thu Jan 01 1970 07:00:01 GMT+0700". You need to use .getFullYear() in onChange , this is the method I tried and it works.

    import { useState } from "react";
    import DatePicker from "react-datepicker";
    
    const [savedate, setSavedate] = useState();
    
    const <your component> = () => {
    
      return() {
            <DatePicker
               id="DatePicker"
               type="string"
               className="text-primary text-center"
               selected={birthYear}
               onChange={(date) => setSavedate(date.getFullYear())}
               showYearPicker
               dateFormat="yyyy"
               yearItemNumber={9}
               required
            />
      }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ru.kongpop