Cron
Try in Playground →Parse and match cron expressions
Examples on this page are verified against the `ts-time-utils` v4.4.1 public exports.
Import
import { ... } from 'ts-time-utils/cron'; Examples
Cron Matching
Check if dates match cron expressions
import { matchesCron, getNextCronDate, isValidCron, CRON_PRESETS } from 'ts-time-utils/cron';
// Validate cron expression
console.log(isValidCron('0 9 * * 1-5')); // true
console.log(isValidCron('invalid')); // false
// Check if date matches
const monday9am = new Date('2025-09-15T09:00:00'); // Monday
console.log(matchesCron(monday9am, '0 9 * * 1')); // true (Mon 9am)
console.log(matchesCron(monday9am, '0 9 * * 5')); // false (not Fri)
// Get next occurrence
const next = getNextCronDate('0 9 * * 1-5'); // Next weekday 9am
console.log('Next weekday 9am:', next);
// Use presets
console.log(CRON_PRESETS.everyDay); // "0 0 * * *"
console.log(CRON_PRESETS.everyWeek); // "0 0 * * 0"
console.log(CRON_PRESETS.everyMonth); // "0 0 1 * *"
console.log(CRON_PRESETS.weekdays); // "0 0 * * 1-5"