使用typeORM Select Query Builder时如何格式化日期?

时间:2019-05-02 02:55:05

标签: javascript typeorm

我在模型中使用@CreateDateColumn()。使用查询生成器进行选择时,如何格式化选择内容中的日期?

  @CreateDateColumn()
  createdAt: Date;
    const documentarys = await getManager()
      .getRepository(IncomingDocumentary)
      .createQueryBuilder('d')
      .leftJoinAndSelect('d.tireBrand', 't')
      .select([
        'd.code as code',
        'd.tireSize as tireSize',
        'd.applicableCarModel as applicableCarModel',
        'd.amount as amount',
        'd.price as price',
        'd.createdAt as createTime',
        't.name as tireBrandName',
      ])
      .getRawMany();

1 个答案:

答案 0 :(得分:0)

使用javascript选择格式后,您可以更轻松地对其进行格式设置,但是,如果您确实要在select查询中对其进行格式设置,则应该使用数据库功能。例如,如果您有基于sql的数据库,则可以这样设置其格式:

const documentarys = await getManager()
  .getRepository(IncomingDocumentary)
  .createQueryBuilder('d')
  .leftJoinAndSelect('d.tireBrand', 't')
  .select([
    'd.code as code',
    'd.tireSize as tireSize',
    'd.applicableCarModel as applicableCarModel',
    'd.amount as amount',
    'd.price as price',
    'TO_CHAR(d.createdAt::DATE, 'dd/mm/yyyy')as createTime',
    't.name as tireBrandName',
  ])
  .getRawMany();