Add method for comparing date against cron pattern
See original GitHub issueI was trying to find a library that allowed my to check the current time against a cron pattern to see if it matches. So far this library is the one closest to what I want, but I had some problems with milliseconds and rounding errors when I tried to convert it to seconds.
The closest I got was to create this function:
function timeMatches(expression, date) {
var interval = parser.parseExpression(expression);
var data = interval._fields;
if (!data.second.includes(date.getSeconds())) {
return false;
}
if (!data.minute.includes(date.getMinutes())) {
return false;
}
if (!data.hour.includes(date.getHours())) {
return false;
}
if (!data.dayOfMonth.includes(date.getDate())) {
return false;
}
if (!data.month.includes(date.getMonth() + 1)) {
return false;
}
if (!data.dayOfWeek.includes(date.getDay())) {
return false;
}
return true;
}
It allows me to do to:
if (timeMatches('* 20 * * * *', new Date()) {
// This is true when minute is 20.
}
Could it be possible to add a method like this to the library? (Not saying my checks are the best, but something to validate current date against pattern).
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
compare Cron Expression with current time - Stack Overflow
I want to check whether cron expression time refer to the time in the future, Otherwise trigger won't be executed at all. Is...
Read more >Construct cron expressions for a filter subscription
This page describes how to construct a cron expression. Cron expressions can be used when creating a subscription to a filter, as described...
Read more >Cron job every month - Crontab.guru
The quick and simple editor for cron schedule expressions by Cronitor. “At 00:00 on day-of-month 1.” next at 2023-01-01 00:00:00.
Read more >croniter · PyPI
This is much like the builtin range(start,stop,step) function, but for dates. The step argument is a cron expression. Added in (>=0.3.34).
Read more >Date functions | BigQuery - Google Cloud
WEEK(<WEEKDAY>) : Returns the week number of the date in the range [0, 53]. Weeks begin on WEEKDAY . Dates prior to the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here’s what I ended up doing. Since you already have a dependency on
moment-timezone
, it’s a pretty lightweight function:Yes I’m not the one to say if this is a feature that belongs here or not. But of all the libraries I found this was the one I thought was the most fitting one, since all the others focus on running functions at cron not parsing the cron string.
But for now I can use my function, so take your time to consider it, and let me know if you need input or something.