简体   繁体   English

使用Sequelize按日期查询时间戳

[英]Querying a timestamp by date with Sequelize

I want to find some entries in my DB through the createdAt column, but just using the date. 我想通过createdAt列在数据库中找到一些条目,但仅使用日期即可。 I am using postgres and the createdAt is a timestamptz . 我正在使用postgres, createdAt是一个timestamptz Here is an example of what an entry in it looks like: 2019-02-27 20:17:07.05+00 这是条目中条目示例的示例: 2019-02-27 20:17:07.05+00

This is what the setting of my query looks like: 这就是我的查询设置:

const dateString = momentDate.format('YYYY-MM-DD')
query.createdAt = { $iLike: `%${dateString}` }

Unfortunately this is not working and I am getting the following error: 不幸的是,这无法正常工作,并且出现以下错误:

retry-as-promised:error SequelizeDatabaseError: operator does not exist: timestamp with time zone ~~* unknown 重试承诺:错误SequelizeDatabaseError:运算符不存在:时区为~~ *的时间戳未知

Is the issue perhaps because I am using a string? 问题可能是因为我正在使用字符串吗? What is the right way to query by date? 按日期查询的正确方法是什么?

Using function DateDiff to get days from between createdAt and dateString . 使用功能则DateDiffcreatedAtdateString之间获得天。 Need to cast dateString to datetime 需要将dateString强制转换为datetime

var sequelize = require('sequelize');

var dateString = '01/03/2019';
yourModel.findAll({
    where: sequelize.literal(`DATEDIFF(day,Cast(${dateString} as datetime), createdAt) = 0`)
})
.then(results => {

})
.catch(err => {

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM