英文:
How to schedule a job between specific time?
问题
我正在尝试使用node-cron安排一个工作,它在上午9:00到下午3:30之间每隔10秒运行一次,从周一到周五,但我无法实现它。这是我的Node Cron代码,目前我可以在上午9:00到下午4:00之间安排,但我想要在上午9:00到下午3:30之间安排,我该如何在node-cron中实现这一点?
const job = cron.schedule('*/10 9-15 * * 1-5', () => {
console.log(new Date());
}, {
timezone: 'Asia/Kolkata'
});
英文:
I'm trying to schedule a job which runs for every 10 seconds between 9:00 AM to 3:30 PM from Monday to Friday using node-cron but I cannout achieve it. Here's my Node Cron code right now I can able to schedule between 9:00 AM to 4:00 PM but I want it from 9:00 AM to 3:30PM, How can I achieve this in node-cron?
const job = cron.schedule('*/1 9-16 * * 1-5', () => {
console.log(new Date());
}, {
timezone: 'Asia/Kolkata'
});
答案1
得分: 1
以下是翻译好的部分:
CRON 1:
*/10
:每10秒*
:每分钟9-15
:从第9小时(09:00 AM)到第15小时(03:00 PM)*
:每天*
:每月1-5
:从周一到周五
CRON 2:
*/10
:每10秒0-30
:从第0分钟到第30分钟15
:在第15小时(03:00 PM)*
:每天*
:每月1-5
:从周一到周五
- Node CRON 文档 在此。
英文:
Following @ashish singh's answer, use two cron jobs:
const cron = require('node-cron')
const job = () => {
console.log(new Date())
}
// Each 10 seconds past every hour from 9 through 15 on every day-of-week from Monday through Friday
cron.schedule('*/10 * 9-15 * * 1-5', () => job())
// Each 10 seconds from 0 through 30 past hour 15 on every day-of-week from Monday through Friday
cron.schedule('*/10 0-30 15 * * 1-5', () => job())
CRON 1:
*/10
: Each 10 seconds*
: Every minute9-15
: From hour 9 (09:00 AM) to 15 (03:00 PM)*
: Every day*
: Every month1-5
: From Monday to Friday
CRON 2:
*/10
: Each 10 seconds0-30
: From minute 0 to 3015
: At hour 15 (03:00 PM)*
: Every day*
: Every month1-5
: From Monday to Friday
* Node CRON documentation here.
答案2
得分: 0
simpler way seems to be using two scheduler
- one for 9 to 3
'0 9-15 * * *'
( this is for only min and hour ) - one for 3 to 3.30
'30 15 * * *'
( this is only for minute and hour)
英文:
simpler way seems to be using two scheduler
- one for 9 to 3
'* 9-15'
( this is for only min and hour ) - one for 3 to 3.30
'0-30 15'
( this is only for minute and hour)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论