How can I create columns with type Date and type DateTime in nestjs with typeORM?

You can see the docs here that explains the @Column decorator. In @Column there is an option called type -> here is where you specify which type of date you want to store for that specific column.

More on column types here.

For example (using PostgreSQL):

@Column({ type: 'date' })
date_only: string;

@Column({ type: 'timestamptz' }) // Recommended
date_time_with_timezone: Date;

@Column({ type: 'timestamp' }) // Not recommended
date_time_without_timezone: Date;

Note that date_only is of type string. See this issue for more information.

Moreover, automatic dates for certain events are possible: 

  • Creation Date, available via @CreateDateColumn() decorator.
  • Last Updated Date, available via @UpdateDateColumn() decorator.
  • Deletion Date, only when soft delete is enabled, available via @DeleteDateColumn() decorator.
created_at: Date; // Creation date

updated_at: Date; // Last updated date

deleted_at: Date; // Deletion date

Leave a Comment