Error: InvalidPipeArgument: 'Unable to convert "27-08-2020" into a date' for pipe 'DatePipe'

2.9k views Asked by At

I need to format a date in an angular application so I use the Date Pipe: I am trying so many custom filter.

I used:

<div class="id_text">{{ splitDate(row?.created_at) | date:'dd-MM-y'}}</div>
 splitDate(date) {
const data = date.split(' ');
return data[0];
}

I Used Angular Filter

<div class="id_text">{{row?.created_at | date:'dd/MM/yyyy'}}</div>

But still getting same error somewhere date does not display I need only date

enter image description here

enter image description here

Response:-

enter image description here

1

There are 1 answers

7
Kamran Khatti On BEST ANSWER

This is because created_at has invalid date string and angular pipe not recognizing it.

All you need to replace this 25-09-2020 11:04:16 with this 2020/2/25 11:04:16, you can do it by writing a date parser method.

  parseDate(date) {
   const parseDate = date.split('-');
   const parseTime = parseDate[2].split(' ');
   const parsedDate = `${parseTime[0]}/${parseDate[1]}/${parseDate[0]} ${parseTime[1]}`

   return parsedDate
 }

Use date parse in template

<div class="id_text">{{ parseDate(row?.created_at) | date:'dd/MM/yyyy'}}</div>