Angular DatePipe to print Datetime [closed]

Tags: , , ,



I get the next object from database:

{
  createdAt: {
    timezone : [...],
    timestamp: 'date value in timestamp'
  }
}

object in json

And i need to print the createdAt property in HTML with Angular 6 so i try next: {{obj.createdAt | date}} but i get the next error: invalidPipeArgument: 'Unable to convert "[object Object]" into a date' for pipe 'DatePipe'

Trying to debug this property with this statement {{obj.createdAt | json}} i get a long json object wich looks like this:

{ "timezone": { "name": "Europe/Berlin",
                "transitions": [..Long content here..]
}

┬┐How can i print the correct dateTime?

Im using Symfony with Doctrine in the Backend and mysql database. The property CreatedAt is a DateTime with this value:

Database result

Answer

The date value is encoded in timestamp;

You have to create a method

toDateIKnow(obj: any) {
    return new Date(obj.createdAt.timestamp);
}

Then in your html file

{{toDateIKnow(obj) | date: 'yyyy-MM-dd'}}


Source: stackoverflow