Croner
Trigger functions or evaluate cron expressions, in JavaScript/TypeScript, Rust, Python, .NET and C++.
Installation Pattern JavaScript usage Try the JavaScript version on JSFiddle
The next five weekdays at 09:30 in Stockholm, in each language. The Rust example uses the chrono-tz crate for the time zone.
import { Cron } from "croner";
const job = new Cron("30 9 * * MON-FRI", { timezone: "Europe/Stockholm" });
console.log(job.nextRuns(5));
use chrono::Utc;
use chrono_tz::Europe::Stockholm;
use croner::Cron;
use std::str::FromStr;
let cron = Cron::from_str("30 9 * * MON-FRI").expect("Invalid pattern");
let now = Utc::now().with_timezone(&Stockholm);
for run in cron.iter_after(now).take(5) {
println!("{run}");
}
from croner import Cron
job = Cron("30 9 * * MON-FRI", timezone="Europe/Stockholm")
print(job.next_runs(5))
using Croner;
var cron = Cron.Parse("30 9 * * MON-FRI");
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Stockholm");
foreach (var run in cron.GetNextOccurrences(DateTimeOffset.UtcNow, tz).Take(5))
{
Console.WriteLine(run);
}
#include <croner/croner.hpp>
#include <iostream>
int main() {
croner::CronOptions opts;
opts.zone = croner::locate_zone("Europe/Stockholm");
const croner::Cron cron("30 9 * * MON-FRI", opts);
for (auto run : cron.next_runs(5)) std::cout << croner::to_iso_string(run) << "\n";
}
Implementations#
Croner is available for several languages. All implementations use the same pattern syntax and the same rules for daylight saving time. Where they differ, for example in which fields accept ?, the Pattern page says so.
| Language | Repository | Package | Evaluate | Schedule | Describe |
|---|---|---|---|---|---|
| JavaScript / TypeScript | croner | npm, JSR | ✓ | ✓ | |
| Rust | croner-rust | crates.io | ✓ | ✓ | |
| Python | croner-py | PyPI | ✓ | ✓ | ✓ |
| .NET | croner-dotnet | NuGet | ✓ | ✓ | ✓ |
| C++ | croner-cpp | Header-only, see Installation | ✓ | ✓ |
Evaluate: find next and previous runs, and check if a date matches. Schedule: run a function on the pattern, with pause, resume, stop and overrun protection. Describe: a human readable description of the pattern, like "At 09:30, on Monday, Tuesday, Wednesday, Thursday, and Friday."
The usage documentation for JavaScript is on this site. The other implementations are documented in the README of each repository.
Features#
- OCPS 1.0-1.4 compliant: Supports the Open Cron Pattern Specification, including seconds and year fields,
L(last),W(weekday),#(nth occurrence), and+(AND logic). DST handling follows Vixie cron instead of the OCPS 1.4 recommendation, the same in every implementation, see Pattern. - Find next and previous runs, or check if a date matches a pattern.
- Target different time zones, with well defined DST behavior.
- Schedule functions (all but Rust), with overrun protection, error handling, pause, resume, stop, max runs, minimum interval, and start and stop dates.
- Operates in-memory, with no need for a database or configuration files.
- No dependencies in JavaScript, Python, .NET and C++. The Rust crate depends on
chronoorjifffor dates and times, and onderive_builderandstrum.
The JavaScript version also:
- Works in Node.js >=18.0 (both require and import), Deno >=2.0 and Bun >=1.0.0.
- Works in browsers as standalone, UMD or ES-module.
- Includes TypeScript typings.
- Supports asynchronous functions.
- Is thoroughly tested and relied upon by well-known projects such as pm2, Uptime Kuma, ZWave JS and TrueNAS.
More JavaScript examples are in Usage (JavaScript), and a comparison with other JavaScript libraries in Comparison (JavaScript).