Skip Holidays in .NET Scheduled Jobs with NaturalCron Custom Keywords
Описание и применение
В .NET с помощью NaturalCron можно объявлять собственные ключевые слова, например "holiday", и использовать их в cron‑выражениях, чтобы автоматически пропускать выходные и праздники.
Задача
Cron expressions cannot express holiday exclusions, forcing jobs to check dates in code and leading to inconsistent schedules.
Результат
The job now schedules only on business days, automatically skipping weekends and defined holidays, with the schedule itself documenting the exclusions.
Рабочий процесс
- Register holiday dates in a HashSet
- Configure NaturalCronCustomKeywords with AddMatchable("holiday", date => holidays.Contains(date.Date))
- Parse a cron expression that includes the custom keyword, e.g. "every day at 09:00:00 except [saturday, sunday, holiday]"
- Call GetNextOccurrences to get future run times
- Observe that holiday dates are skipped automatically
Доказательства
With NaturalCron 1.1, you can finally teach your .NET jobs what a holiday is.
A schedule like “every weekday at 9, but not on holidays” is easy to say and hard to configure.
You register it once, at application startup
Christmas Day, the weekends and New Year’s Day are skipped, and the schedule itself says why.
- Дата записи: 2026-10-14T04:41:31.583Z