How to run cronjob specific time (UTC) with Node-Cron?

3.4k Views Asked by At

I use node-cron library for working with schedule task. I want to run my task every 1hr ( 00.00, 01.00, 02.00...24.00) in timezone of UTC but it doesn't work ( time was incorrect)

Thank you.

3

There are 3 best solutions below

0
On

You need to define the time you want. You can use this website to do this.

Or use this example :

var cron = require('node-cron');

cron.schedule("0 * * * *", () => { // 0 * * * * = every houre at minute 0
    console.log("running a task every houre");
});
0
On

// If you want to run it at a specific time it can be like this:

// It will run every day at 10:36:00 AM

const cron = require('node-cron');
cron.schedule('00 36 10 * * *', () => {
    console.log(new Date());
});
0
On

From the docs:

See IANA time zone database for valid values, such as Asia/Shanghai, Asia/Kolkata, America/Sao_Paulo.

You can see list of tz database time zones at wikipedia.

let task24 = cron.schedule('0 * * * *' async () => {
    // task
}, { timezone: 'Etc/UTC' })

Update

There's a memory leak bug if you set the node-cron's options.timezone. Unless the issue is fixed, use croner instead. Here's how to migrate from node-cron to croner.