Rabbani_
Rabbani_

Reputation: 470

Sequlize FindAll condition by today's date

I have field in my sequlize Model that serving created timestamp. see below

activity_timestamp:{
    type: DataTypes.DATE
},

Now I want to get all date that created today..

 let condition = { source_operator_id: req.user_info.operator_id, activity_status: true }
    if( currentDate){
        condition.activity_timestamp = new Date();
    }
   TrollyActivityLog.findAll({
        where: condition,
        include:[
            {
                model:CustomerProfile,
                as:'customerinfo',
                required:true,
            }
        ],
        limit: limit,
        offset: offset 

    })

I want to get all data created today.

Upvotes: 1

Views: 99

Answers (1)

CyberEternal
CyberEternal

Reputation: 2545

You can o it in the following way. In my example, I have used a moment.js, but of course, you can get the start and end of the day using the JS functionality.

let condition = {
  source_operator_id: req.user_info.operator_id,
  activity_status: true,
};

// For the case when you want to get a  data for the specific date
// if (date) {
//   condition.activity_timestamp = {
//     [Op.and]: [
//       {
//         [Op.gte]: moment(date)
//           .startOf("day")
//           .format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
//       },
//       {
//         [Op.lte]: moment(date)
//           .endOf("day")
//           .format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
//       },
//     ],
//   };
// }

// If you want to get a data for the current day
condition.activity_timestamp = {
  [Op.and]: [
    {
      [Op.gte]: moment().startOf("day").format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
    },
    {
      [Op.lte]: moment().endOf("day").format("YYYY-MM-DDTHH:mm:ss.SSS[Z]"),
    },
  ],
};

TrollyActivityLog.findAll({
  where: condition,
  include: [
    {
      model: CustomerProfile,
      as: "customerinfo",
      required: true,
    },
  ],
  limit: limit,
  offset: offset,
});

Upvotes: 1

Related Questions