You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeORM中使用timestamp类型createdAt字段查询无结果求助

问题解决:日期匹配不到数据库数据的原因及方案

问题根源

数据库中createdAt字段存储的是包含毫秒的完整时间戳,而你手动格式化后的日期仅保留到秒级别,导致精确匹配时因毫秒差异无法命中数据。此外,时区不一致也可能导致时间显示偏差。

解决方案

方案1:使用日期范围查询(推荐,适合匹配某天/某时间段的数据)

如果需求是查询指定日期当天的所有数据,直接用时间范围过滤,避免精确匹配的问题:

// 构造当天的起始和结束时间
const startOfDay = new Date(date.getFullYear(), date.getMonth(), date.getDate());
const endOfDay = new Date(startOfDay);
endOfDay.setDate(endOfDay.getDate() + 1);

const bookedRoomEntities = await this.createQueryBuilder('booking')
    .where(`booking.roomId = :roomId`, { roomId })
    .andWhere(`booking.createdAt >= :startOfDay AND booking.createdAt < :endOfDay`, { startOfDay, endOfDay })
    .orderBy('booking.start')
    .getMany();

方案2:忽略毫秒,匹配到秒级

如果必须精确到秒匹配,可以将数据库的时间转换为秒级字符串后对比(注意适配不同数据库的函数):

  • PostgreSQL 使用 TO_CHAR:
const formattedDate = format(date, 'yyyy-MM-dd HH:mm:ss');
const bookedRoomEntities = await this.createQueryBuilder('booking')
    .where(`booking.roomId = :roomId`, { roomId })
    .andWhere(`TO_CHAR(booking.createdAt, 'YYYY-MM-DD HH24:MI:SS') = :date`, { date: formattedDate })
    .orderBy('booking.start')
    .getMany();
  • MySQL 使用 DATE_FORMAT:
const formattedDate = format(date, 'yyyy-MM-dd HH:mm:ss');
const bookedRoomEntities = await this.createQueryBuilder('booking')
    .where(`booking.roomId = :roomId`, { roomId })
    .andWhere(`DATE_FORMAT(booking.createdAt, '%Y-%m-%d %H:%i:%s') = :date`, { date: formattedDate })
    .orderBy('booking.start')
    .getMany();

方案3:标准化日期对象的毫秒部分

手动将查询用的日期毫秒设为0,确保和数据库存储的时间(如果毫秒被截断的话)匹配:

const dateWithoutMs = new Date(date);
dateWithoutMs.setMilliseconds(0);

const bookedRoomEntities = await this.createQueryBuilder('booking')
    .where(`booking.roomId = :roomId`, { roomId })
    .andWhere(`booking.createdAt = :date`, { date: dateWithoutMs })
    .orderBy('booking.start')
    .getMany();

额外检查:时区一致性

确认数据库的时区和应用程序的时区是否一致。比如数据库用UTC存储,而应用程序使用本地时间,会导致时间差。可以在TypeORM的连接配置中指定时区:

// ormconfig.ts 或连接配置
{
  type: 'postgres', // 或其他数据库类型
  host: 'localhost',
  // ...其他配置
  timezone: 'UTC' // 或你的本地时区,比如 'Asia/Shanghai'
}

内容的提问来源于stack exchange,提问作者Eugene09

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 14:42:54