Skip to content
项目

关于

Human-readable schedule expressions for .NET (C#): write "every day at 9am except sunday" instead of cron. Time zones, fluent builder, custom keywords, cron converter.

开发工具

这些数字如何统计

可以做什么

  • Readable syntax: every 30 minutes in [jan, jun] between 09:00 and 18:00
  • Fluent Builder API: Strongly typed for .NET developers.
  • No online generators needed.
  • Well tested: more than 3,600 automated tests run on every commit.

实测星标增长

24小时
+4
7天
正在收集历史
15天
正在收集历史
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

NaturalCron

NuGet .NET Tests License: MIT Awesome Sponsor

NaturalCron is a human-readable scheduling library for .NET (C#): a natural-language alternative to cron expressions. It lets you write schedules in a clear and intuitive way instead of memorizing cryptic cron strings.

Why? Because memorizing 0 18 * * 1-5 is harder than understanding every day between monday and friday at 6:00pm.

Readable schedules reduce mistakes, write expressions that you can understand at a glance.

Note: NaturalCron is not a cron converter. It is a self-contained scheduling engine with its own expressive syntax. If you need a classic cron string for an external system, the optional NaturalCron.CronConverter package can translate expressions — but be aware that some NaturalCron features have no cron equivalent.

💡 Why use NaturalCron?

  • Readable syntax: every 30 minutes in [jan, jun] between 09:00 and 18:00
  • Fluent Builder API: Strongly typed for .NET developers.
  • No online generators needed.
  • Well tested: more than 3,600 automated tests run on every commit.
  • Features:
    • Ranges
    • Weekday list
    • Closest weekday, first/last day handling
    • Time zone support with IANA TZ names
    • Exclusions with except
    • Your own keywords, such as holiday

🚀 Quick Start

Using an Expression

using System;
using NaturalCron;

// Define an advanced expression
string expression = "every 30 minutes in [jan, jun] between 09:00 and 18:00";

// Parse and calculate next occurrence (local time)
NaturalCronExpr schedule = NaturalCronExpr.Parse(expression);
DateTime next = schedule.GetNextOccurrence(DateTime.Now);

Console.WriteLine($"Next occurrence: {next}");

Using Fluent Builder

using System;
using NaturalCron;
using NaturalCron.Builder;

NaturalCronExpr schedule = NaturalCronBuilder
.Every(30).Minutes()
.In(NaturalCronMonth.Jan)
.Between("09:00", "18:00")
.Build();

DateTime next = schedule.GetNextOccurrenceInUtc(DateTime.UtcNow);

Console.WriteLine($"Next occurrence in UTC: {next}");

⚡ Try it online: Run on .NET Fiddle

Running a Job on a Schedule

NaturalCron tells you when the next run is; you decide how to run it. In an ASP.NET Core or Worker app, a BackgroundService is enough:

using Microsoft.Extensions.Hosting;
using NaturalCron;

public class DailyReportJob : BackgroundService
{
    private readonly NaturalCronExpr schedule = NaturalCronExpr.Parse(
        "every day at 09:00:00 except [saturday, sunday] tz America/New_York");

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            DateTime? next = schedule.TryGetNextOccurrenceInUtc(DateTime.UtcNow);
            if (next == null)
            {
                break; // no more occurrences
            }

            TimeSpan delay = next.Value - DateTime.UtcNow;
            if (delay > TimeSpan.Zero)
            {
                await Task.Delay(delay, stoppingToken);
            }

            await SendReportAsync(stoppingToken); // your job
        }
    }
}
builder.Services.AddHostedService<DailyReportJob>();

Need persistence, retries or a dashboard? See JobMaster, or convert the expression to a cron string for Hangfire, Quartz.NET or NCrontab with the Cron Converter.

🆚 Cron vs NaturalCron

Task Cron Expression NaturalCron Expression
Every 5 minutes */5 * * * * every 5 minutes
Every weekday at 6 PM 0 18 * * 1-5 every day between mon and fri at 18:00
Every 30 min in Jan and Jun (9:00-18:00) (Complex in cron) every 30 minutes in [jan, jun] between 09:00 and 18:00

📝 Syntax Overview

Examples:

every 5 minutes
every day at 18:00
every 30 minutes in [jan, jun] between 09:00 and 18:00
every day at 10:00 on [monday, wednesday, friday]
every day at 09:00 except [saturday, sunday]

every can also be followed directly by a value, as a shorthand:

every 9am                     same as: every day at 9am
every 15th at 18:00           same as: every month on 15th at 18:00
every 1stMonday at 09:00      same as: every month on 1stMonday at 09:00
every [monday, friday] at 09:00
every january on 1st at 00:00

Custom keywords

Define your own keywords once at startup and use them in any expression:

using NaturalCron.CustomRules;

NaturalCronCustomKeywords.Configure(cfg =>
    cfg.AddMatchable("holiday", date => myHolidays.Contains(date.Date)));
every holiday at 09:00              every holiday
every day on holiday at 09:00       the same, written in full
every day at 09:00 except holiday   every day but the holidays
every day on [holiday, 15th] at 09:00

📦 Installation

dotnet add package NaturalCron

📖 Documentation

  • Expression Syntax — Learn how to write human-readable recurrence rules.
  • Fluent Builder Guide — Build expressions easily with type safety and IntelliSense.
  • API Reference — Full API details and usage examples.
  • Cron Converter — Translate NaturalCron expressions to classic cron strings, and what is not supported.

🔌 JobMaster Integration (Alpha)

Looking for a complete job scheduling solution with NaturalCron support? Check out JobMaster — a powerful .NET job scheduling library that natively integrates with NaturalCron for human-readable scheduling.

🤝 Contributing

Contributions, bug reports, and feature requests are welcome!

Please open an issue or submit a pull request.

探索相关项目

加载中…