最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Getting datetimes from cron format using javascript - Stack Overflow

programmeradmin1浏览0评论

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

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
Share Improve this question edited Oct 24, 2011 at 20:10 Jonathan M 17.5k9 gold badges60 silver badges94 bronze badges asked Oct 24, 2011 at 15:19 Jordan WallworkJordan Wallwork 3,1142 gold badges25 silver badges51 bronze badges 5
  • Crontag? Tpyo[sic]? You want to parse the "* */4 * * * *"-type crontab entries and turn that into real dates/times? – Marc B Commented Oct 24, 2011 at 15:23
  • Adding to @MarcB, ...or a range of dates/times? – Jonathan M Commented Oct 24, 2011 at 15:26
  • The way I get it, the input is: startdate, enddate and a crontab line. The output shall be the exact datetimes when the crontab command will run between the two given dates. – kapa Commented Oct 24, 2011 at 15:28
  • Yeah exactly, 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 – Jordan Wallwork Commented Oct 24, 2011 at 15:28
  • Updated the question based on the OP's comment. – kapa Commented Oct 24, 2011 at 15:30
Add a comment  | 

3 Answers 3

Reset to default 20

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);

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

One can use cron-parser lib. More info here

发布评论

评论列表(0)

  1. 暂无评论