带日期的Where语句的后缀 [英] Sequelize Where statement with date

查看:24
本文介绍了带日期的Where语句的后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sequelize作为后端ORM.现在,我希望对Date执行一些操作.

I am using sequelize as my backend ORM. Now i wish to do some where operations on a Date.

更多具体地说,我想获取日期为现在和7天后的所有数据.

More Speceficly i want to get all data where a date is from now and 7 days back.

问题是文档没有指定您可以对 Datatypes.DATE

The problem is that the documentation does not specify which operations you can do on Datatypes.DATE

有人能指出我正确的方向吗?

Can anyone point me in the right direction?

推荐答案

就像摩尔达说的那样,您可以使用 $ gt $ lt $ gte $ lte 并带有日期:

Just like Molda says, you can use $gt, $lt, $gte or $lte with a date:

model.findAll({
  where: {
    start_datetime: {
      $gte: moment().subtract(7, 'days').toDate()
    }
  }
})

如果您使用的是Sequelize v5,则必须包含 Op ,因为密钥已移至 Symbol

If you're using v5 of Sequelize, you've to include Op because the key was moved into Symbol

const { Op } = require('sequelize')

model.findAll({
  where: {
    start_datetime: {
      [Op.gte]: moment().subtract(7, 'days').toDate()
    }
  }
})

在此处进行序列化文档

这篇关于带日期的Where语句的后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆