moment-recur
moment-recur is a recurring date plugin for momentjs. This plugin handles dates only; time information is discarded.
The rule matching concept is borrowed from the excellent node-date-recur library created by Andrew Chilton.
var rInterval = moment( "01/01/2014" ).recur().every(2).days();
rInterval.matches( "01/03/2014" );
var rCalendar = moment.recur().every(10).dayOfMonth();
rCalendar.matches( "05/10/2014" );
Getting Started
moment-recur can be included in your project a few different ways.
Browser
Simply include the momentjs script, then the moment-recur script.
<script src="moment.min.js"></script>
<script src="moment-recur.js"></script>
Browser with RequireJS
moment-recur also works with RequireJS. Include it just like any other script.
define(["moment", "moment-recur"], function(moment){
});
Bower
moment-recur is a register bower component.
bower install moment-recur
node.js
moment-recur can be installed with npm and required into a script.
npm install moment-recur
var moment = require('moment');
require('moment-recur');
Creating a Recurring Date
You can create a recurrence from an instance of moment or from the constructor a few different ways.
From an instance:
var recurrence;
recurrence = moment().recur();
recurrence = moment().recur( start, end );
recurrence = moment(start).recur( end );
recurrence = moment().recur({
start: "01/01/2014",
end: "01/01/2015"
});
From the constructor:
var recurrence;
recurrence = moment.recur();
recurrence = moment.recur( start, end );
recurrence = moment.recur({
start: "01/01/2014",
end: "01/01/2015"
});
Creating Rules
moment-recur uses rules to define when a date should recur. You can then generate future or past recurrences based on these rules, or see if a specific date matches the rules. Rules can also be overridden or removed.
The every() Function
The every()
function allows you to set the units and, optionally, the measurment type of the recurring date. It returns the recur object to allow chaining.
var myDate, recurrence;
myDate = moment("01/01/2014");
recurrence = myDate.recur().every(1, "days");
recurrence = myDate.recur().every(1).day();
recurrence = myDate.recur().every([3, 5]).days();
recurrence = myDate.recur().every(["Monday", "wed"]).daysOfWeek();
recurrence = myDate.recur().every(["Jan", "february"], "monthsOfYear");
every()
will override the last "every" if a measurement was not provided. The following line will create a recurrence for every 5 days.
recurrence = myDate.recur().every(1).every(5).days();
If you need to specify multiple units, pass an array to every()
.
You may also pass the units directly to the interval functions (listed below) instead of using every()
.
var recurrence = moment.recur().monthOfYear("January");
Length Intervals
moment-recur supports intervals for days, weeks, months, and years. Measurements may be singular or plural (ex: day()
vs days()
). Length Intervals must have a start date defined.
Possible Length Intervals Include:
- day / days
- week / weeks
- month / months
- year / years
Examples
var myDate, interval;
myDate = moment("01/01/2014");
interval = myDate.recur().every(1).day();
interval = myDate.recur().every(2).weeks();
interval = myDate.recur().every(3).months();
interval = myDate.recur().every(1).years();
interval = myDate.recur().every([3, 5]).days();
interval = myDate.recur().every(3).days().every(2).months();
Calendar Intervals
Calendar Intervals do not depend on a start date. They define a unit of another unit. For instance, a day of a month, or a month of a year. Measurements may be singular or plural (ex: dayOfMonth()
vs daysOfMonth()
).
Possible Calendar Intervals Include:
- dayOfWeek / daysOfWeek
- dayOfMonth / daysOfMonth
- weekOfMonth / weeksOfMonth
- weekOfYear / weeksOfYear
- monthOfYear / monthsOfYear
Examples
var cal;
cal = moment.recur().every(["Sunday", 1]).daysOfWeek();
cal = moment.recur().every([1, 10]).daysOfMonth();
cal = moment.recur().every([1, 3]).weeksOfMonth();
cal = moment.recur().every(20).weekOfYear();
cal = moment.recur().every("January").monthsOfYear();
var valentines = moment.recur().every(14).daysOfMonth()
.every("Februray").monthsOfYear();
cal = moment.recur().every("Thursday").daysOfWeek()
.every([0, 2]).weeksOfMonthByDay();
cal = moment.recur().every(moment("01/01/2014").day()).daysOfWeek()
.every(moment("01/01/2014").monthWeekByDay()).weeksOfMonthByDay();
Using the Rules
Matching
The matches()
function will test a date to check if all of the recurrence rules match. It returns true
if the date matches, false
otherwise.
var interval = moment("01/01/2014").recur().every(2).days();
interval.matches("01/02/2014");
interval.matches("01/03/2014");
You may also see if a date matches before the start date or after the end date by passing true
as the second argument to matches()
.
var interval = moment("01/01/2014").recur().every(2).days();
interval.matches("12/30/2013");
interval.matches("12/30/2013", true);
Exceptions
To prevent a date from matching that would normally match, use the except()
function.
var recurrence = moment("01/01/2014").recur().every(1).day().except("01/02/2014");
recurrence.matches("01/02/2014");
Overriding and Forgetting
If a rule is created with the same measurement of a previous rule, it will override the previous rule. Rules can also be removed from a recurrence.
var recurrence = moment("01/01/2014").recur().every(1).day().except("01/03/2014");
recurrence.every(2).days();
recurrence.forget("01/03/2014");
recurrence.forget("days");
Generating Dates
It is also possible to generate dates from the rules. These functions require a starting date.
var recurrence, nextDates;
recurrence = moment("01/01/2014").recur().every(2).days();
nextDates = recurrence.next(3);
nextDates = recurrence.next(3, "L");
nextDates = recurrence.previous(3, "L");
If your recurrence does not have a start date set, or if it does but you want to start at a different date, use the fromDate()
method first.
var recurrence = moment("01/01/2014").recur().every(2).days();
recurrence.fromDate("02/05/2014");
nextDates = recurrence.next(3, "L");
With both a start date and an end date set, you can generate all dates within that range that match the pattern (including the start/end dates).
var recurrence = moment().recur("01/01/2014", "01/07/2014").every(2).days();
allDates = recurrence.all("L");
Important Note: These functions may be very inefficient/slow. They work by attempting to match every date from the start of a range until the desired number of dates have been generated. So if you attempt to get 10 dates for a rule that matches once a year, it will run the match function for ~3650 days.
Options and Other Methods
moment-recur provides a few methods for getting/setting options, as well as two utility methods. It also creates two additional momentjs functions.
Options
Options can be set when creating a recurrence or using the getter/setter methods listed below.
Set options upon creation. Note that the units for rules are converted to objects, so it is not recommended to set rules this way. They can be set in the options so that they can be imported.
moment().recur({
start: "01/01/2014",
end: "12/31/2014",
rules: [
{ units: { 2 : true }, measure: "days" }
],
exceptions: ["01/05/2014"]
});
Get/Set the Start Date
recurrence.startDate();
recurrence.startDate("01/01/2014");
Get/Set the End Date
recurrence.endDate();
recurrence.endDate("01/01/2014");
Get/Set a temporary "From Date" for use with generating dates
recurrence.fromDate();
recurrence.fromDate("01/01/2014");
Utility Methods
Use repeats()
to check if a recurrence has rules set.
recurrence.repeats();
Use save()
to export all options, rules, and exceptions as an object. This can be used to store recurrences in a database.
Note: This does not export the "From Date" which is considered a temporary option.
recurrence.save();
momentjs Functions
The monthWeek()
method can be used to determine the week of the month a date is in.
moment("01/01/2014").monthWeek();
The dateOnly()
method can be used to remove any time information from a moment.
moment("2014-01-01 09:30:26").dateOnly();
License
UNLICENSE - see UNLICENSE file and unlicense.org for details.