Search code examples
reactjsreact-datepicker

react-datepicker gives wrong values


I am building an application and I am using react-datepicker however i have a strange behavior of the component. The selected date shows wrong format. Here is what i am using:

this.state={
   selectedDate: moment().format(),
}

and

<DatePicker selected={this.state.selectedDate}/>

The problem is that the component shows the below value: "52//02/2019/".

It seems like the component is counting how many days have passed till the current date in the year. I've also tried moment().format('DD/MM/YYYY') with no luck. any help would be appreciated. Thanks


Solution

  • Okay i am posting this as an answer to show what worked for me. I believe this is too big to be a comment.

    So I set:

    this.state={selectedDate:moment().format()}
    

    and in the component i used:

    <DatePicker selected={this.state.selectedDate} 
       dateFormat={moment(this.state.selectedDate).format('DD/MM/YYYY')}/>
    

    This way i was able to change the selected date and get the current date value instead of the days count. However I am still not sure of how great this solution is. At least it works for me for now. Hope it helps others.