Trigger functions or evaluate cron expressions in JavaScript or TypeScript. No dependencies. All features. Node. Deno. Bun. Browser.
Try it live on jsfiddle.
Features
- Trigger functions in JavaScript using Cron syntax.
- Evaluate cron expressions and get a list of upcoming run times.
- Uses Vixie-cron pattern, with additional features such as
L
for last day and weekday of month and#
for nth weekday of month. - Works in Node.js >=18.0 (both require and import), Deno >=1.16 and Bun >=1.0.0.
- Works in browsers as standalone, UMD or ES-module.
- Target different time zones.
- Built-in overrun protection
- Built-in error handling
- Includes TypeScript typings.
- Support for asynchronous functions.
- Pause, resume, or stop execution after a task is scheduled.
- Operates in-memory, with no need for a database or configuration files.
- Zero dependencies.
- Thoroughly tested and is relied upon by well-known projects such as pm2, Uptime Kuma, ZWave JS and TrueNAS.
Quick examples
Run a function at the interval defined by a cron expression
const job = new Cron('*/5 * * * * *', () => {
console.log('This will run every five seconds');
});
What dates do the next 100 sundays occur on?
const nextSundays = new Cron('0 0 0 * * 7').nextRuns(100);
console.log(nextSundays);
Days left to a specific date
const msLeft = new Cron('59 59 23 24 DEC *').nextRun() - new Date();
console.log(Math.floor(msLeft/1000/3600/24) + " days left to next christmas eve");
Run a function at a specific date/time using a non-local timezone
Time is ISO 8601 local time, this will run 2024-01-23 00:00:00 according to the time in Asia/Kolkata
new Cron('2024-01-23T00:00:00', { timezone: 'Asia/Kolkata' }, () => { console.log('Yay!') });
More examples at usage/examples.md
Feature comparison
In this comparison, we outline the key differences between Croner and four other popular scheduling libraries: cronosjs, node-cron, cron, and node-schedule. The libraries are compared across various features such as platform compatibility, functionality, error handling, and Typescript support.
The table below provides a brief overview of each library's features.
croner | cronosjs | node-cron | cron | node-schedule | |
---|---|---|---|---|---|
Platforms | |||||
Node.js (CommonJS) | ✓ | ✓ | ✓ | ✓ | ✓ |
Browser (ESMCommonJS) | ✓ | ✓ | |||
Deno (ESM) | ✓ | ||||
Features | |||||
Over-run protection | ✓ | ||||
Error handling | ✓ | ✓ | |||
Typescript typings | ✓ | ✓ | ✓ | ||
Unref timers (optional) | ✓ | ✓ | |||
dom-OR-dow* | ✓ | ✓ | ✓ | ✓ | ✓ |
dom-AND-dow* (optional) | ✓ | ||||
Next run | ✓ | ✓ | ✓ | ✓ | |
Next n runs | ✓ | ✓ | ✓ | ||
Timezone | ✓ | ✓ | ✓ | ✓ | ✓ |
Minimum interval | ✓ | ||||
Controls (stop/resume) | ✓ | ✓ | ✓ | ✓ | ✓ |
Range (0-13) | ✓ | ✓ | ✓ | ✓ | ✓ |
Stepping (*/5) | ✓ | ✓ | ✓ | ✓ | ✓ |
Last day of month (L) | ✓ | ✓ | |||
Nth weekday of month (#) | ✓ | ✓ |
DOM and DOW?
DOM stands for Day of Month, and DOW stands for Day of Week.