Issue
I'm trying to find a way how to get a specific value from a CRON expression.
E.g. I have this CRON expression "*/5 * * * *"
and I need to get minutes. The result must be 5. All I found it's example of usage NCrontab library. It shows how to get schedule:
var schedule = CrontabSchedule.Parse("*/5 * * * *");
Is there any way to solve this problem?
Solution
If you simply need the minutes, you shouldn't need the NCrontab library. You can parse this simple enough yourself.
var expression = "*/5 * * * *";
var split = expression.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (split.Length < 5)
throw new Exception("Invalid CRON expression");
var onlyDigits = "0123456789";
// strip non numeric chars
var minutes = new string(split[0].Where(c => onlyDigits.Contains(c)).ToArray());
Answered By - Cory Answer Checked By - Robin (WPSolving Admin)