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