CronExpressions - any librarys out there to generate them/convert them into human readable form?
Asked Answered
M

4

27

I am using Quartz.NET, and my scheduler relies heavily on the use of cron expression's - such as the ones detailed on this link:

http://quartznet.sourceforge.net/tutorial/lesson_6.html

Ideally, I'd like the 2 scenarios to be made possible (the 1st is probably much difficult, and less likely to be anything out there for it)

1: The ability for the user to construct a cron expression on an ASP.NET form, where they choose which minutes, hours, days etc

2: The ability to turn a cron expression into a short string, such as 'Every {x} minutes on Friday' or 'At {x} o clock on the last day of every month'

I've begun writing my own versions of these, but it's a very hefty task and I'd love to find out there's a library out there, or even to have any hints and tips on this off you guys.

Note: If there is nothing out there for this and I am (relatively) successful in creating the code, I'll gladly share the source if anyone wants it.

Matteroffact answered 23/6, 2011 at 10:55 Comment(1)
Please post your version of the library. It will be usefull to me. ThanksTinytinya
M
40

I wrote a C# library that does just this. It's called cron-expression-descriptor. https://github.com/bradymholt/cron-expression-descriptor.

Malleus answered 22/2, 2012 at 16:26 Comment(3)
Do you know if there's a tool to do the opposite? Enter in "At 11:30 AM, Monday through Friday" and have it return: "30 11 * * 1-5"Czarevna
@JeremyThompson for simple expressions, GPT4 can do this. Complex expressions it can get wrong.Houppelande
@PēterisCaune I reckon I could copy this comment and put it under almost any other comment and it would still be valid!Czarevna
S
14

Using the latest Quartz.NET (2.1 as of this writing) you can get the Cron expression by using the CronScheduleBuilder.

Getting the expression for daily jobs:

var t = CronScheduleBuilder.DailyAtHourAndMinute(10, 0).Build() as CronTriggerImpl;
Console.WriteLine(t.CronExpressionString);// This is the actual Cron
Console.WriteLine(t.GetExpressionSummary()); // This is the description of that Cron
Swamper answered 14/7, 2012 at 4:8 Comment(1)
This website was built using Quartz: link, might be a useful example for who wants to build expressions from human readable GUI.Corporative
C
4

I ended up using this jquery plugin which provides a nice interface and then using a hidden field to save this to a database in .net/c#.

http://shawnchin.github.com/jquery-cron/

It works quite well for me. You may need to check that you are setting seconds at the front of the string to "0" though if using quartz.net though like I was as this plugin doesn't support seconds.

Classless answered 7/2, 2013 at 23:20 Comment(0)
A
2

I used NCrontab a while ago http://www.raboof.com/Projects/NCrontab/

But it seems it does not translate it to Human Readable...

Ayana answered 23/6, 2011 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.