简体   繁体   中英

Query by date with column that is a datetime

How can I query for all records of a certain date if the column is a datetime?

This doesn't work. Is there a preferred way to do this in sequelize?

startDate and endDate are datetime 's

Thing.findAll({
  where: {
    startDate = {
      [Op.gte]: '02-20-2020'
    },
    endDate = {
      [Op.lte]: '02-20-2020'
    },
  }
});

Split them and create a javascript Date object before using that straight in the query parameters.

const [month, day, year] = '02-20-2020'.split('-')
const date = new Date(year, month, day)

Thing.findAll({
  where: {
    startDate = {
      [Op.gte]: date
    }
  }
});

See Several ways to create a Date object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM