0

How to fetch all records within the same day using deliveryDate as parameter.

For example when passing 2023-02-13T12.23.00 as a parameter, I should get records that fall within that same day i.e 2023-02-13T09.23.00, 2023-02-13T16.01.00, regardless of the timestamps using Sequelize.

Here is the query I am using:

const result = await Order.findOne({
    where: {
      leaderId: leaderId,
      deliveryDate: deliveryDate
    }
  });
tmarwen
  • 15,750
  • 5
  • 43
  • 62
rai8
  • 1
  • This can help you https://stackoverflow.com/questions/53415686/compare-timestamp-with-date-in-sequelize-query – Caio Filus Feb 13 '23 at 12:46

1 Answers1

0

You should use the the sequelize query operators combining a lower and higher Date ranges:

let deliveryDate; // some `deliveryDate` being a Date object (make sure to format it properly and have the desired timezone)
cont low = new Date(now.getFullYear(), now.getMonth(), now.getDate())
const high = low = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);

const result = await Order.findOne({
    where: {
      [Op.and] : [
        {
          leaderId: leaderId
        },
        {
          deliveryDate: {
              [Op.lt]: high,
            [Op.gt]: low
          }
        }
      ]
    }
});
tmarwen
  • 15,750
  • 5
  • 43
  • 62