I have got a project where I need to ingest an API feed here http://am-twitter-scrape.herokuapp.com/users/Twitter?pages_limit=3&wait=0
example date string: 2:21 PM - 19 Jul 2019
I need cut off the time and reformat the date like “Month Day, Year” (e.g. Jan 5, 2019).
I have tried to run the datePipe over the string variable like so:
<ng-container matColumnDef="date">
<th mat-header-cell *matHeaderCellDef> Date </th>
<!--<td mat-cell *matCellDef="let hashtags"> {{hashtags.date | date: 'longDate'}} </td>-->
<td mat-cell *matCellDef="let hashtags"> {{hashtags.date}} </td>
</ng-container>
But it just turns up an error saying
InvalidPipeArgument: ‘Unable to convert “2:21 PM – 19 Jul 2019” into a date’ for pipe ‘DatePipe’
Which seems like the date is in a format incompatible with the included Angular toolkit to change the format.
What could be my possible options? Can I use Regex in Angular?
3
Answers
Then you need to use
splitDate
in your pipe to format it, if it didn’t work then wrap it withnew Date(splitDate)
Yes, you can use regex in Angular. However, an easier solution could be using string operations.
In your template, you can use:
And then create the
convertDate
function in your component:You can split the data and parse it using the Date object or momentJs. Once the date is parsed you can change it into any format. For example in your component,
In your html, you can use,