我想检查所购买产品的过期日期,如果过期,我想将其移除。
保存到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();
我正在获取数据:
let userCourses = await usersPurchasedCourses.find({
$and: [{ username: req.body.username }, { status: "VALID" }],
});
这里我想要一个只有有效数据的列表。没有过期的。我该怎么做呢?
仅使用大于条件与当前日期使用$gt: new Date()
,
let userCourses = await usersPurchasedCourses.find({
username: req.body.username,
status: "VALID",
expirationDate: { $gt: new Date() }
});
moment.js运行良好
if(moment(new Date()).isSameOrAfter(new Date(item.expirationDate))){
console.log("expired");
} else {
item.coursesList.map((item2) => {
userallcourses.push(item2);
});
}