How to add a where statement with date with Sequelize?

Sometimes, we want to add a where statement with date with Sequelize.

In this article, we’ll look at how to add a where statement with date with Sequelize.

How to add a where statement with date with Sequelize?

To add a where statement with date with Sequelize, we can use a JavaScript date object in where.

For instance, we write

const {
  Op
} = require('sequelize')

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

to query for items with the start_datetime value greater than or equal to 7 days or later than today with

[Op.gte]: moment().subtract(7, 'days').toDate()

We use the Op.gte operator to search for a column greater than or equal to a value.

Conclusion

To add a where statement with date with Sequelize, we can use a JavaScript date object in where.