41

I am new with nestjs. How can I set columns that accepts Date format and dateTime format?

Not in both cases, the columns are two differents column, one accept Date and other dateTime.

peterh
  • 11,875
  • 18
  • 85
  • 108
kri-dev
  • 699
  • 2
  • 7
  • 12

4 Answers4

95

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: 

@CreateDateColumn()
created_at: Date; // Creation date

@UpdateDateColumn()
updated_at: Date; // Last updated date

@DeleteDateColumn()
deleted_at: Date; // Deletion date
Carlo Corradini
  • 2,927
  • 2
  • 18
  • 24
  • 2
    This answer is not correct. The type returned by typeorm for `type: 'date'` is a string - not a date object with the time set to 0. See https://github.com/typeorm/typeorm/issues/2176 – sarfata Mar 27 '21 at 02:05
  • 1
    @sarfata I've updated my answer. Let me know what you think! – Carlo Corradini Mar 27 '21 at 14:52
  • 2
    @CarloCorradini awesome! I like that you clearly recommend `timestamptz` too ;) – sarfata Mar 27 '21 at 18:04
  • would timestamp be ok when using mysql? as timestamptz is not supported? – Jeremy Dec 30 '21 at 12:04
  • Ok, but how to use "timestamptz" with GraphQL? What should I use inside ObjectType and InputType to fix the issue with the wrong DateTime types? – Eugene Zalivadnyi Jul 03 '22 at 13:24
  • @EugeneZalivadnyi I use [GraphQL Scalars](https://github.com/Urigo/graphql-scalars) library with a date/time scalar mapping. E.g. `GraphQLTimestamp` for UNIX epoch or `GraphQLDateTime` for UTC. You decide what best suits your needs since the input given to the scalar is always a `Date` instance (`timestamptz`). – Carlo Corradini Jul 03 '22 at 14:01
  • @CarloCorradini any reason why "timestamptz" is recommended over "timestamp"? – bilard Nov 20 '22 at 01:37
  • @bilard See https://stackoverflow.com/a/22925669/6676781 – Carlo Corradini Nov 20 '22 at 01:57
  • Anyone who comes here and doesn't find how to include the columns https://stackoverflow.com/questions/64941148/node-js-add-created-at-and-updated-at-in-entity-of-typeorm – Stephen Himes Apr 28 '23 at 13:57
46

How about ?

@CreateDateColumn()
created_at: Date;
    
@UpdateDateColumn()
updated_at: Date;

EDIT

You can find more info here

Luis Contreras
  • 777
  • 9
  • 23
13

To add more to this ticket as DateTime was not even mentioned:

  /**
   * Start DateTime
   */
  @Column({
    type: 'datetime',
    default: () => 'NOW()',
  })
  @Index()
  start: string;

  /**
   * End DateTime
   */
  @Column({
    type: 'datetime',
    nullable: true,
  })
  @Index()
  end: string;

This is for those not wanting or needing to use

@CreateDateColumn()

@UpdateDateColumn()

Jeremy
  • 1,878
  • 2
  • 30
  • 54
3

This is how I do it. The name parameter represents the column name in the database. If you use the built in decorators it will handle updating the columns in without additional code.

@CreateDateColumn({ name: 'created_at'})
createdAt: Date;

@UpdateDateColumn({ name: 'updated_at' })
updatedAt: Date;

@DeleteDateColumn({ name: 'deleted_at' })
deletedAt: Date;
aaronmbmorse
  • 359
  • 3
  • 10