Sequelize Where statement with date

前端 未结 4 1908
野的像风
野的像风 2020-12-14 14:01

I am using sequelize as my backend ORM. Now i wish to do some where operations on a Date.

More Speceficly i want to get all data where a date is from now and 7 days

4条回答
  •  萌比男神i
    2020-12-14 14:33

    Just like Molda says, you can use $gt, $lt, $gte or $lte with a date:

    model.findAll({
      where: {
        start_datetime: {
          $gte: moment().subtract(7, 'days').toDate()
        }
      }
    })
    

    If you're using v5 of Sequelize, you've to include Op because the key was moved into Symbol

    const { Op } = require('sequelize')
    
    model.findAll({
      where: {
        start_datetime: {
          [Op.gte]: moment().subtract(7, 'days').toDate()
        }
      }
    })
    

    See more sequelize documentation here

提交回复
热议问题