ngaotruyen ngaotruyen - 4 months ago 17
Javascript Question

later.js doesn't get next datetime of cron expression

I use library later.js to get value next datetime from cron expression. But result's 5 times next isn't incorrect with time default ("7/16/2015 10:44:47"). This Result is deviation "hour".
this is my code.

<script src="../later.min.js" type="text/javascript"></script>
<script>
var cron = "0 0/10 7,8,9,10,11,13,14,15,16,17,18 ? 1/1 1-5 *";

var d = new Date("7/16/2015 10:44:47");
later.date.timezone(true);
var sched = later.parse.cron(cron, true);

var results = later.schedule(sched).next(5,d);
for (var i = 0; i < results.length; i++) {
document.write('<p>' + results[i].toLocaleString() + '</p>');
}
</script>


result incorrect :


14:00:00 16/7/2015

14:10:00 16/7/2015

14:20:00 16/7/2015

14:30:00 16/7/2015

14:40:00 16/7/2015


result correct use http://www.cronmaker.com/ to test:



  1. Thursday, July 16, 2015 10:50 AM

  2. Thursday, July 16, 2015 11:00 AM

  3. Thursday, July 16, 2015 11:10 AM

  4. Thursday, July 16, 2015 11:20 AM

  5. Thursday, July 16, 2015 11:30 AM



Answer

I found answer for my question. Therefore, I post solution to refer.

In file later.js, It has function 'itemSorter' (line 999). this function sort array from crop expression (after it split ' ' and ',').

function itemSorter(a, b) {
    return isHash(a) && !isHash(b) ? 1 : 0;
}

Change this function to:

  function itemSorter(a, b) {
        if (isNaN(a) || isNaN(b)) {
            return a > b ? 1 : -1;
        }
        return a - b;
    }

if you don't have later.js, you can download from URL: https://github.com/bunkat/later.