Parse and match cron expressions

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.DAILY);    // "0 0 * * *"
console.log(CRON_PRESETS.WEEKLY);   // "0 0 * * 0"
console.log(CRON_PRESETS.MONTHLY);  // "0 0 1 * *"
console.log(CRON_PRESETS.WEEKDAYS); // "0 0 * * 1-5"