uniCloud 中如何查询时间戳在本月之内的数据

请添加图片描述

👨🏻‍💻 热爱摄影的程序员
👨🏻‍🎨 喜欢编码的设计师
🧕🏻 擅长设计的剪辑师
🧑🏻‍🏫 一位高冷无情的编码爱好者
大家好,我是 DevOps 工程师
欢迎分享 / 收藏 / 赞 / 在看!

【需求】查询创建时间在本月之内的记录

【解决】首先,数据中 create_date 字段存储时间戳,注意是13位,如果是其他位数要经过转换,否则查询结果对不上。

[{
    "type": 1,
    "create_date": 16828704000000, // 2503-4-14 0:0:0
    "user_id": "0ce2",
    "comment": "芭蕾舞课程到课"
},{
    "type": 1,
    "create_date": 1682872200000, // 2023-5-1 0:30:0
    "user_id": "0ce2",
    "comment": "拉丁舞课程到课"
},{   
    "type": 1,
    "create_date": 1682874000000, // 2023-5-1 1:0:0
    "user_id": "0ce2",
    "comment": "芭蕾舞课程到课"
}]

其次,使用查询语句获取数据。

const currentDate = new Date() // Mon May 01 2023 02:00:51 GMT+0800 (中国标准时间)
const year = currentDate.getFullYear() // 2023
const month = currentDate.getMonth() // 4
const firstDayOfMonth = new Date(year, month, 1) // Mon May 01 2023 00:00:00 GMT+0800 (中国标准时间)
const lastDayOfMonth = new Date(year, month + 1, 0) // Wed May 31 2023 00:00:00 GMT+0800 (中国标准时间)
db.collection('your-collection').where({
	user_id: '0ce2',
	create_date: db.command.gte(firstDayOfMonth.getTime()).and(db.command.lte(lastDayOfMonth
		.getTime()))
}).get()

最终结果:

[
    {
        "_id": "9d7d",
        "comment": "拉丁舞课程到课",
        "create_date": 1682872200000,
        "type": 1,
        "user_id": "0ce2"
    },
    {
        "_id": "eb87",
        "comment": "芭蕾舞课程到课",
        "create_date": 1682874000000,
        "type": 1,
        "user_id": "0ce2"
    }
]

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程洪同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值