I'm trying to use isToday
method of date-fns
to filter records that were saved today in the database. Can I do this within the sequelize query? Below the way I'm trying to do:
import { isToday } from 'date-fns';
import Buy from '../models/Buy';
class VolumeController {
// Exibir o total de Bitcoins comprados e vendidos no dia
async show(req, res) {
const buys = await Buy.findAll({
where: {
created_at: isToday(created_at),
},
attributes: ['id'', 'buy_value', 'quantity', 'created_at'],
});
return res.json({ buys });
}
}
I solved it as follows. I used the startOfDay
and endOfDay
methods of the date-fns
library. I also need the between
operator from the sequelize
library.
import { Op } from 'sequelize';
import { startOfDay, endOfDay } from 'date-fns';
import Buy from '../models/Buy';
class VolumeController {
// Exibir o total de Bitcoins comprados e vendidos no dia
async show(req, res) {
const today = new Date();
const buys = await Buy.findAll({
where: {
created_at: {
[Op.between]: [startOfDay(today), endOfDay(today)],
},
},
attributes: ['id', 'account_id', 'buy_value', 'quantity', 'created_at'],
});
return res.json({ buys });
}
}
export default new VolumeController();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.