Checking Your Cron Expression’s Schedule

If you’re using the CronTrigger to fire your Quartz.Net job, then you’ll have to come up with a CronExpression that generates the schedule you want. Sometimes you may not be sure if it’s the correct schedule or maybe Quartz.Net doesn’t fire when you want it to. There are multiple reasons why a trigger doesn’t fire when it should but perhaps the first thing to look at is whether you’ve got the correct CronExpression.

You may be wondering, is there a way to test this? Well, yes, that’s what this post is about. Let’s get right into it.

Quartz.Net relies on a class called CronExpression to generate the schedule defined by the string cron expression you pass to your trigger. That class is accessible to you, so you could write code that looks like this to generate the next ten fire times:

public List<DateTimeOffset> Schedule(string cronExpression)
{
	CronExpression expresion = new CronExpression(cronExpression);
    var lastTime = DateTimeOffset.Now;
	var times = new List<DateTimeOffset>();
	for (int i = 0; i < 10; i++)
	{
		var nextTime = expresion.GetNextValidTimeAfter(lastTime);
		if (nextTime.HasValue)
		{
			times.Add(nextTime.Value);
			lastTime = nextTime.Value;
		}
		else
		{
			break;
		}
		}
	return times;
}

I’ve created a simple page that you can use to show the schedule generated by the Quartz.Net cron expression. It’s just the basics right now but I’ll enhance it as I get requests or as I need it to do more for me. Let me know if you find it useful (or not)!