apicloud根据时间段查询

在apicloud中每张表都有createdAt和updatedAt字段,是date型的,一般时间段查询就根据该字段进行查询。
这里关键点有两个:

  1. “与”查询,关键字是and
  2. 是大于等于开始时间,小于等于结束时间

结合以上两个关键点,查询的where部门写法如下:

"where":{
    "and":[//逻辑与
            {"createdAt":{"gte":'2018-05-16 00:00:00'}},//gte大于等于
            {"createdAt":{"lte":'2018-05-16 23:59:59'}}//lte小于等于
        ]
}

为什么要加上23:59:59?

由于原字段值里有时分秒,所以此处也要加上23:59:59秒,否则默认按照00:00:00查询,即按照2018-05-16 00:00:00查询,当然查询不到2018-05-16这一天的数据

完整代码如下:

function queryList(){
        var client = new Resource("", "");
        var Model = client.Factory("Plan");
        Model.query({
            filter:{
                fields:{"id": true, "Ptitle": true, "Pclassification": true,"Pnote":true,"createdAt":true},
                "where":{
                            "and":[
                        {"createdAt":{"gte":'2018-05-16 00:00:00'}},
                            {"createdAt":{"lte":'2018-05-16 23:59:59'}}
                            ]
                    }
            }
        }, function(ret,err){
            if(err){
                //处理错误 errmi
                alert(JSON.stringify(err));
            }else{
                //处理数据 ret
                alert(JSON.stringify(ret));
            }
        })
    }

原文出处:https://malaoshi.top/show_1EF1AMJ2QJ8v.html