Skip to content

Getting started

jcoutch edited this page Jan 14, 2016 · 2 revisions

To get started with using NCrontab.Advanced, include the Nuget package in your project, and add the following:

using NCrontab.Advanced;
using NCrontab.Advanced.Enumerations;

To parse a cron string using default cron strings (i.e. what base NCrontab supports):

var cronInstance = CrontabSchedule.Parse("* * * * *");

To parse a cron string using a different format:

var cronInstance = CrontabSchedule.Parse("* * * * * *", CronStringFormat.WithSeconds);

To get the next instance of a date:

var cronInstance = CrontabSchedule.Parse("* * * * *");
var input = DateTime.Parse("2015-01-01 00:00:00");
var nextOccurrence = cronInstance.GetNextOccurrence(input);

To get all instances between a range:

var cronInstance = CrontabSchedule.Parse("* */4 * * *");
var start = DateTime.Parse("2015-01-01 00:00:00");
var end = DateTime.Parse("2015-01-02 00:00:00");
var nextOccurrence = cronInstance.GetNextOccurrences(input);

There are additional samples that you can play around with over at .NET Fiddle! If by chance the fiddle is down, here's the source for the demo:

using System;
using NCrontab.Advanced;
using NCrontab.Advanced.Enumerations;

public class Program
{
	public static void Main()
	{
		var input = DateTime.Parse("2015-1-1 00:00:00");
		
		// Match everything
		WriteOutput("* * * * *", input, CronStringFormat.Default);
		
		// Match everything, with resolution to the second
		WriteOutput("* * * * * *", input, CronStringFormat.WithSeconds);
		
		// Last day of the month
		WriteOutput("* * L * *", input, CronStringFormat.Default);
		
		// Last weekday of the month
		WriteOutput("* * LW * *", input, CronStringFormat.Default);
		
		// Every 5 minutes on the last weekday of the month
		WriteOutput("*/5 * LW * *", input, CronStringFormat.Default);
		
		// Every 5 minutes offset by 3 minutes (:03, :08, :13, etc.) on the last weekday of the month
		WriteOutput("3/5 * LW * *", input, CronStringFormat.Default);
		
		// Every 5 minutes offset by 3 minutes (:03, :08, :13, etc.) on the nearest weekday to the 17th of the month
		WriteOutput("3/5 * 17W * *", input, CronStringFormat.Default);

		// At 8:30 AM on the weekday nearest to the 17th, but only if it's a Thursday, and limit the search to be between 2000 and 2050
		// (Since there are 10 instances that match with the provided inputs, the 11th will be the endValue, which by default is DateTime.MaxValue)
		WriteMultipleOutput(11, "30 8 17W Jan,February 4 2000-2050", input, CronStringFormat.WithYears);
	}
	
	public static DateTime WriteOutput(string cron, DateTime input, CronStringFormat format) {
		var formatName = Enum.GetName(typeof(CronStringFormat), format);
		var parser = CrontabSchedule.Parse(cron, format);
		var result = parser.GetNextOccurrence(input);
		
		Console.WriteLine("{0}    {1}    {2}    {3}", cron.PadRight(30), formatName.PadRight(15), input.ToString().PadLeft(25), result.ToString().PadLeft(25));
		return result;
	}
	
	public static void WriteMultipleOutput(int count, string cron, DateTime input, CronStringFormat format) {
		var newInput = input;
		Console.WriteLine("\n{0} instances of {1} starting at {2}:", count, cron, input);
		
		for(var i = 0; i < count; i++) {
			newInput = WriteOutput(cron, newInput, format);
		}
	}
}
Clone this wiki locally