Home > Back-end >  Date Pipe value in Angular - 'Unable to convert "20:28:08.146604 00:00" into a date&#
Date Pipe value in Angular - 'Unable to convert "20:28:08.146604 00:00" into a date&#

Time:01-23

I want to make a Date pipe to an attribut of an object (Note object) in Angular. here's the Model:

export class Notes{
    constructor(public post_name: string, public content: string
        , public post_id: number, public user_id: number,
        public user_name: string, public email: string,
         public id_user: number,
        public created_at : Date 
         ){}
}

here is the code that fetch the data from the API (I store the result in list of type Notes):

ShowNotes(){            
        return  this.http.get<Notes[] | [] >(this.server '/posts')
}

the schema in my API of the data :

class All_details(BaseModel):
       post_name: str
       content: str
       post_id: int
       user_id: int
       user_name:str
       email: str
       id_user: int
       created_at :time

the created_at attribut is generated automaticaly in Postgres and it's of type time with time zone when I do tests with Postmanthe created_at has this form "created_at": "17:45:57.639281 00:00"

here is how I try to make the Pipe

<p > {{post.created_at | date }} </p>

I understand that the API send a string format but normally when I have defiend created_at as Date type, typescript must convert the string format into Date

I'm not sure what causing the problem (Angular or the API) so I used them both in the tags.

CodePudding user response:

You cannot format that as a Date because the date part of the created_at timestamp is missing -- it's just the time. The way your Postgres is set up just discards important information, so start by changing that to store the date part as well. See https://www.postgresql.org/docs/9.1/datatype-datetime.html, you probably need timestamp with time zone, or maybe just timestamp (without time zone).

  • Related