问题描述
我使用 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 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)