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,824
  • 17
  • 99
  • 186
Pranay kumar
  • 1,983
  • 4
  • 22
  • 51

2 Answers2

2

You should use toLocaleDateString like the following:

onChange = date => {
    console.log(date.toLocaleDateString());
    this.setState({ date: date.toLocaleDateString() });
}
Omid Nikrah
  • 2,444
  • 3
  • 15
  • 30
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
  • 22,578
  • 22
  • 103
  • 117