Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting datetimes from cron format using javascript

Does anyone know of any existing solutions using javascript that can parse a crontab and return all datetime instances between a given start and end date?

ie if i have 0 * * * *, start 24/10/2011 16:00 and end 24/10/2011 19:00 then it will return:

24/10/2011 16:00,
24/10/2011 17:00,
24/10/2011 18:00
like image 643
Jordan Wallwork Avatar asked Oct 24 '11 15:10

Jordan Wallwork


People also ask

What does 30 * * * * mean in crontab?

*/30 * * * * your_command. this means "run when the minute of each hour is evenly divisible by 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc) example #3. 0,30 * * * * your_command. this means "run when the minute of each hour is 0 or 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)

What does * 5 * * * mean in cron?

Run a Cron Job Every 5 Minutes However, typing the whole list can be tedious and prone to errors. The second option to specify a job to be run every 5 minutes hours is to use the step operator: */5 * * * * command. */5 means create a list of all minutes and run the job for every fifth value from the list.

Is cron expression in UTC?

io's cron expression feature, the designated time zone is UTC. Each field has a set of allowed values, which are the only values that are valid to use for that particular field.

What is cron time format?

Cron job format A schedule is defined using the unix-cron string format ( * * * * * ) which is a set of five fields in a line, indicating when the job should be executed. You can use either the Google Cloud console, the Google Cloud CLI, or the Cloud Scheduler REST API to set your schedule.


2 Answers

You might want to check out Later.js which can parse a Cron expression and calculate the next occurrences.

var schedule = cronParser().parse('* */5 * * * *', true);
var results = later(60).get(schedule, 100, startDate, endDate);
like image 170
Bill Avatar answered Oct 02 '22 14:10

Bill


This isn't much help, but it's a start. There are some java (not javascript) and php solutions that have some decent code that you'd want to translate and incorporate if you end up writing this yourself.

Take a look at these two bits of code:

http://www.redmoon.ch/?p=39

http://www.phpclasses.org/package/2568-PHP-Parse-cron-tab-files-to-retrieve-job-schedules.html

HTH

like image 24
Jonathan M Avatar answered Oct 02 '22 13:10

Jonathan M