1

I used the react calendar for displaying the calendar. Currently when I try to access the date from its onChange method.

onChange = date => {
        console.log(date); //Sun Jul 19 2020 00:00:00 GMT+0530 (India Standard Time)
        this.setState({ date });
    }

I want to change this date format in something like this (07/18/2020).

I tried with the formatLongDate as per their documentation as I wasn't able to get the desired result.

halfer
  • 19,471
  • 17
  • 87
  • 173
Pranay kumar
  • 1,665
  • 2
  • 16
  • 35

2 Answers2

1

You should use toLocaleDateString like the following:

onChange = date => {
    console.log(date.toLocaleDateString());
    this.setState({ date: date.toLocaleDateString() });
}
Omid Nikrah
  • 2,307
  • 3
  • 12
  • 27
0

You can try using toLocaleDateString:

onChange = date => {
        console.log(date); //Sun Jul 19 2020 00:00:00 GMT+0530 (India Standard Time)
        if (date instanceof Date) {
        this.setState({ date: date.toLocaleDateString()});
        }
}
Melchia
  • 19,807
  • 18
  • 90
  • 103