javascript 使用typeORM Select Query Builder时如何设置日期格式?

cbjzeqam  于 2023-03-28  发布在  Java
关注(0)|答案(1)|浏览(249)

我在模型中使用@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();
tyu7yeag

tyu7yeag1#

在使用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();

相关问题