gpt4 book ai didi

node.js - 使用sequelize比较 Node js中的日期

转载 作者:行者123 更新时间:2023-12-03 22:35:25 25 4
gpt4 key购买 nike

Actually I am using sqlite database(tables). Node.js for back-end. Now I need to get the records that fall under two specific dates.(for example 24.04.2020 11.00 AM to 28.04.2020 02.00 pm) How I do this? in what format I need to store the date and time in database? and how I compare?

最佳答案

Sequelize 使用 js 日期。所以你可以做这样的事情:

const { Op } = require("sequelize");

Foo.findAll({
where: {

// myDateColumn < [timestamp] AND myDateColumn > [timestamp]
{
myDateColumn: {
[Op.lt]: new Date(),
[Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000)
}
},


// myDateColumn in range
[Op.notBetween]: [new Date(), new Date(new Date() - 24 * 60 * 60 * 1000)]
}
});

我还建议使用 moment.js 或 luxon(个人最喜欢的)来处理日期。

使用 luxon 格式化日期:
const DateTime = require('luxon');

// the reason we love luxon is because it's easy to set the timezone
const startDate = DateTime.local().setZone('America/New_York').fromISO('2020-04-24T11:00:00').toJSDate();


在此处阅读更多信息: https://sequelize.org/master/manual/model-querying-basics.html

关于node.js - 使用sequelize比较 Node js中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61517012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com