How to get data between today and 7 days before from sequelize?

Viewed 10

I create it like this. I couldn't get value for the start date.

var datetime = new Date();
let endDate = (datetime.toISOString().slice(0,10));  // today
const startDate= endDate -7; // 7 days before

const places = await Places.findAll({
    where: {createDate: {between : [endDate, startDate]}}   
 } 
);
1 Answers

You can add or subtract whole days from the third parameter of new Date(...):

const startDate = new Date(datetime.getFullYear(),
                           datetime.getMonth(),
                           datetime.getDate() - 7).toISOString().slice(0,10);

This works even if the current date is, say, the 6th of a month. The result will then be the last-but-one day of the previous month.

Related