Sequelize Where statement with date(Sequelize 带有日期的 Where 语句)
问题描述
我使用 Sequelize 作为我的后端 ORM.现在我想在日期上做一些 WHERE
操作.
I am using Sequelize as my backend ORM. Now I wish to do some WHERE
operations on a Date.
更具体地说,我想获取日期介于现在和 7 天前之间的所有数据.
More specifically, I want to get all data where a date is between now and 7 days ago.
问题是文档没有指定可以对 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?
推荐答案
就像 Molda 说的,你可以使用 $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()
}
}
})
在此处查看更多续集文档
这篇关于Sequelize 带有日期的 Where 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sequelize 带有日期的 Where 语句


基础教程推荐
- Chart.js 在线性图表上拖动点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01