How to get record if createdDate is from one year later using sequelize
userId createdDate
1. 2023-07-19 00:49:59
2. 2022-08-20 00:49:59
Tried
return this.findAll({
createdDate: {
[Op.gte]: new Date(new Date().getFullYear(), 1)
}
});
Expected Record
2. 2022-08-20 00:49:59
2
Answers
You calculate the
oneYearAgo
date by subtracting one year from the current date’s year and keeping the same month, day, hours, minutes, and seconds. Then, we use theOp.gte
operator to find records greater than or equal to oneYearAgo, and theOp.lt
operator to find records less thancurrentDate
. This way, we retrieve records with acreatedDate
falling within the last one year.EDIT based on question:
should subtract 1 year from the current date.
from this: https://stackoverflow.com/a/33070496/15557206
you can get date + 1 full year with: