I want to check the expiration date of the purchased product and if the date is expired then I want to remove it.
The way I’m saving it to DB:
let currentDate = new Date();
let currentDateMiliseconds = currentDate.getTime();
let courseExpiresMiliseconds =
currentDateMiliseconds + 90 * 24 * 60 * 60 * 1000; // 90 Days validity
let courseExpires = new Date(courseExpiresMiliseconds);
let userPurchasedCourses = new usersPurchasedCourses({
username: `${value_a}`,
coursesList: JSON.parse(value_c.replaceAll(".", '"')),
expirationDate: `courseExpires`,
tran_date: tran_date,
status: "VALID"
});
await userPurchasedCourses.save();
I’m getting the data:
let userCourses = await usersPurchasedCourses.find({
$and: [{ username: req.body.username }, { status: "VALID" }],
});
Here I want to have a list of only valid data. No expired ones. How may I do that?
2
Answers
moment.js worked well for me.
Simply use greater than condition with current date using
$gt: new Date()
,