A TypeScript wrapper for node-cron that provides a clean API for managing scheduled tasks with advanced features like task lifecycle management, type-safe cron expression building, and timezone support.
Returns: Object with success, message, and optionally task properties.
import { JsCron } from '@kreisler/js-cron';const cron = new JsCron({ timezone: 'America/Bogota' });// Execute every 15 minutes between 6:00 and 23:00const result = cron.createTask( 'dailyReport', '0 */15 6-23 * * *', () => { console.log('Generating daily report...'); // Your task logic here });if (!result.success) { console.error(result.message);}
The createTask method validates the cron expression before creating the task. If the expression is invalid or a task with the same name already exists, it returns an error message.
Valid values: 0-59, *, ranges, steps, predefined patterns like '0,15,30,45'
// Common patterns with IntelliSenseminute: '0,15,30,45' // Every quarter hourminute: '0,30' // Every half hourminute: '*/5' // Every 5 minutesminute: '10,44' // At minutes 10 and 44
Valid values: 0-23, *, ranges, steps, predefined patterns like '9,12,15'
// Common patterns with IntelliSensehour: '9,17' // 9 AM and 5 PMhour: '14,18' // 2 PM and 6 PMhour: '9-17' // Business hourshour: '0,6,12,18' // Every 6 hours
Valid values: 1-31, *, ?, L (last), W (weekday), ranges, steps
dayOfMonth: '1' // First day of monthdayOfMonth: '1,15' // 1st and 15thdayOfMonth: 'L' // Last day of monthdayOfMonth: '15W' // Nearest weekday to 15thdayOfMonth: 'LW' // Last weekday of month
// Every 15 minutes from 6 AM to 11 PMcron.createTask('business', '0 */15 6-23 * * *', callback);// Every hour during business hours (9 AM - 5 PM)cron.createTask('business2', '0 0 9-17 * * MON-FRI', callback);// At 9 AM and 5 PM on weekdayscron.createTask('business3', '0 0 9,17 * * 1-5', callback);
Daily/Weekly/Monthly tasks
// Daily at 8:30 AMcron.createTask('daily', '0 30 8 * * *', callback);// Every Monday at 9 AMcron.createTask('weekly', '0 0 9 * * MON', callback);// First day of every month at midnightcron.createTask('monthly', '0 0 0 1 * *', callback);// Last day of every month at 11:59 PMcron.createTask('monthEnd', '0 59 23 L * *', callback);
Advanced schedules
// Every quarter (Jan, Apr, Jul, Oct) at 1st day, 9 AMcron.createTask('quarterly', '0 0 9 1 JAN,APR,JUL,OCT *', callback);// Last Friday of every month at 5 PMcron.createTask('lastFriday', '0 0 17 * * 5L', callback);// Every weekday at 10 and 3cron.createTask('weekday', '0 0 10,15 * * MON-FRI', callback);