Search code examples
reactjsredux

TypeError: date.getDate is not a function ReactJS


I have a date picker and basically, it returns the format like this: YYYY-MM-DD

But I need to parse it to format "DD/MM/YYYY" and I am using this function: function

convertDateFormat(date) {
    return String(date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear())
}

So also I am calling this function when I dispatch my data

dispatch(Order(convertDateFormat(inputs.datePickerdate)));

but I am getting this error: TypeError: date.getDate is not a function Do you have any idea that I can do it in react?


Solution

  • It appears your date argument is being passed in as a string rather than a JavaScript Date object. You can either pass in a Date object directly or handle this inside the convertDateFormat function as follows:

    convertDateFormat(dateString) {
      const date = new Date(dateString);
    
      return String(date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear())
    }