const sendReport = new CronJob(
"0 0 28-31 * *",
async function () {
try {
console.log("Running a task every minute in sending Reports");
await sendMail();
} catch (error) {
console.error("Error running Sending Email:", error);
}
},
null,
true,
"UTC"
);
在这个“0 0 28-31 *”上,我暂时这样分配它,但我该如何制作它,只会在该月的最后一天发送,我已经尝试使用 crontab 但它不适用于我的代码,请帮我解决这个问题我该怎么办?
您不需要更改 cron 表达式。 您可以在 Node.js 函数中检查日期,例如:
const sendReport = new CronJob(
"0 0 28-31 * *",
async function () {
const today = new Date();
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
// Check if tomorrow's date is the 1st of the next month
if (tomorrow.getDate() === 1) {
try {
console.log("Running task to send reports on the last day of the month");
await sendMail();
} catch (error) {
console.error("Error running Sending Email:", error);
}
}
},
null,
true,
"UTC"
);