Quick start

Step 1: Install

Later is available through both npm and bower . After installing, Later is available in the later namespace.

Using npm and Node.js:

$ npm install later

var later = require('later');

Using bower and a browser:

$ bower install later

<script src="later.min.js" type="text/javascript"></script> <script type="text/javascript"> // later namespace is available </script>

Step 2: Define a schedule

The first step is to create a new schedule definition. There are a bunch of different ways to define a schedule, but at the end of the day they are just simple json objects that can easily be serialized and stored.

// define a new schedule var textSched = later.parse.text('at 10:15am every weekday'); var cronSched = later.parse.cron('0 0/5 14,18 * * ?'); var recurSched = later.parse.recur().last().dayOfMonth(); var manualSched = {schedules: [{M: 3, D: 21}]};

Schedules can also get as complicated as you need:

// this schedule will fire on the closest weekday to the 15th // every month at 2:00 am except in March var complexSched = later.parse.recur() .on(15).dayOfMonth().onWeekday().on(2).hour() .and() .on(14).dayOfMonth().on(6).dayOfWeek().on(2).hour() .and() .on(16).dayOfMonth().on(2).dayOfWeek().on(2).hour() .except() .on(3).month();

Step 3: Configure timezone

All schedule definitions are completely timezone agnostic. When you need to calculate occurrences, you can decide to perform the calculation using local time or UTC.

// set later to use UTC (the default) later.date.UTC(); // set later to use local time later.date.localTime();

Step 4: Calculate occurrences

Once a schedule has been defined, calculating future (or past) occurrences is very straightforward. You can calculate as many occurrences as you need in either a forward or backward direction. You can even calculate ranges with start and end dates which allows scheduling of appointments or events.

// calculate the next 10 occurrences of a recur schedule var recurSched = later.parse.recur().last().dayOfMonth(); next = later.schedule(recurSched).next(10); // calculate the previous occurrence starting from March 21, 2013 var cronSched = later.parse.cron('0 0/5 14,18 * * ?'), next = later.schedule(cronSched).prev(1, new Date(2013, 2, 21));

More information on calculating individual instances and ranges.

Step 5: Execute code using a Later schedule

Later includes implementations of setTimeout() and setInterval() that work with Later schedules instead of milliseconds. This makes it extremely easy to execute code based on a schedule definition.

// will fire every 5 minutes var textSched = later.parse.text('every 5 min'); // execute logTime one time on the next occurrence of the text schedule var timer = later.setTimeout(logTime, textSched); // execute logTime for each successive occurrence of the text schedule var timer2 = later.setInterval(logTime, textSched); // function to execute function logTime() { console.log(new Date()); } // clear the interval timer when you are done timer2.clear();

More information on setTimeout and setInterval.