【问题标题】:Sequelize - how to compare equality between datesSequelize - 如何比较日期之间的相等性
【发布时间】:2020-08-20 06:15:31
【问题描述】:

如何比较两个日期之间的相等性? 似乎以下不起作用:

const result: SomeModel= SomeModel.findOne( {where: 
            {
                startTime : {
                    [Op.eq] : someDateTime
                }
            }
        });

【问题讨论】:

  • 这取决于 startTime 和 someDateTime 的类型。添加模型定义和此列的 DB 类型以及 someDateTime 中的值。同时指出您正在使用的 wgat DBMS
  • startTime 是日期类型。 someDateTime 是一个 Date 对象。我正在使用 PostgreSQL 进行生产。用于单元测试 SQLite。
  • 相等比较的两边都包含一个时间部分,这就是它们不相等的原因。尝试使用这样的东西: Sequelize.where(Sequelize.fn('date', Sequelize.col('startTime')), Sequelize.fn('date', someDateTime)) (请记住,sqlite 可能不支持这个函数)
  • 这是否也会比较 Date 变量的时间部分?我想比较日期和时间。两者应该相等。
  • 在这种情况下,您应该检查并比较数据库和变量中的时区。他们平等吗? PostreSQL DB 中的列的确切类型是什么?有时区还是没有时区?

标签: javascript node.js typescript orm sequelize.js


【解决方案1】:

请尝试

  const result: SomeModel= SomeModel.findOne( {where: 
                {
                    startTime : {
                        [Op.and] : someDateTime
                    }
                }
            });

这里是所有可以执行的操作的列表

Project.findAll({
  where: {
    id: {
      [Op.and]: {a: 5},           // AND (a = 5)
      [Op.or]: [{a: 5}, {a: 6}],  // (a = 5 OR a = 6)
      [Op.gt]: 6,                // id > 6
      [Op.gte]: 6,               // id >= 6
      [Op.lt]: 10,               // id < 10
      [Op.lte]: 10,              // id <= 10
      [Op.ne]: 20,               // id != 20
      [Op.between]: [6, 10],     // BETWEEN 6 AND 10
      [Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15
      [Op.in]: [1, 2],           // IN [1, 2]
      [Op.notIn]: [1, 2],        // NOT IN [1, 2]
      [Op.like]: '%hat',         // LIKE '%hat'
      [Op.notLike]: '%hat',       // NOT LIKE '%hat'
      [Op.iLike]: '%hat',         // ILIKE '%hat' (case insensitive)  (PG only)
      [Op.notILike]: '%hat',      // NOT ILIKE '%hat'  (PG only)
      [Op.overlap]: [1, 2],       // && [1, 2] (PG array overlap operator)
      [Op.contains]: [1, 2],      // @> [1, 2] (PG array contains operator)
      [Op.contained]: [1, 2],     // <@ [1, 2] (PG array contained by operator)
      [Op.any]: [2,3]            // ANY ARRAY[2, 3]::INTEGER (PG only)
    },
    status: {
      [Op.not]: false           // status NOT FALSE
    }
  }
})

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 2013-04-09
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    相关资源
    最近更新 更多