【问题标题】:Sequlize FindAll condition by today's date按今天的日期对 FindAll 条件进行续集
【发布时间】:2021-05-28 09:43:53
【问题描述】:

我的 sequlize 模型中有服务创建时间戳的字段。见下文

activity_timestamp:{
    type: DataTypes.DATE
},

现在我想获取今天创建的所有日期..

 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 

    })

我想获取今天创建的所有数据。

【问题讨论】:

    标签: express sequelize.js


    【解决方案1】:

    您可以通过以下方式进行操作。在我的示例中,我使用了moment.js,但当然,您可以使用 JS 功能获取一天的开始和结束。

    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,
    });
    

    【讨论】:

    • 我已经实现了,但它显示的是空数据
    • 请分享您在数据库中的数据。也请分享您使用过的currentDate
    • 2021-05-28 11:05:29.866748 这个日期在我的数据库中。我需要保存今天创建的所有数据
    • 据我了解,您没有定义的 currentDate 变量。让我编辑答案。
    • @Rabbani_ 请立即查看
    猜你喜欢
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    相关资源
    最近更新 更多